Zergman Posted July 31, 2008 Share Posted July 31, 2008 Something weird happened today. Had this in my page $date = date ('Y-m-d'); $backmonth = date("Y-m-d", strtotime("-1 month")); Was working yesterday, now these variables give me this 2008-07-31 2008-07-01 Im guessing its my server thats causing this .... not sure where they are located actually but can anybody explain whats going on? Tried doing this $date = date ('Y-m-d'); $backmonth = date("Y-m-d", strtotime("-2 month")); Gives me this 2008-07-31 2008-05-31 This isn't making sense. Why does -1 month return 30 days which lies within the same current month, but -2 works fine? I know my server time is 2hrs ahead of me btw Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/ Share on other sites More sharing options...
JD* Posted July 31, 2008 Share Posted July 31, 2008 Sounds like the date function is programmed properly to know the difference between months with 30 days and 31 days Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604664 Share on other sites More sharing options...
ikmyer Posted July 31, 2008 Share Posted July 31, 2008 to get previous months... i use: <?php $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y")); $lastmonth = date("Y-m-d", mktime(0, 0, 0, date("m")-1, date("d"), date("Y"))); //for your Y-m-d format ?> Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604672 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 http://bugs.php.net/bug.php?id=44073 It's known. There are a few functions here that'll fix it. http://php.net/manual/en/function.strtotime.php Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604674 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 to get previous months... i use: $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y")); <?php $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y")); $date = date ('Y-m-d', $lastmonth); echo $date; ?> Returns 2008-07-01 Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604678 Share on other sites More sharing options...
ikmyer Posted July 31, 2008 Share Posted July 31, 2008 sorry, actually look at some of my code this time... here is my full list... note this creates a range like 2008-06-01:2008-06-30 <?php $day = date("d"); $month = date("m"); $year = date("Y"); $today = date("Y-m-d").":".date("Y-m-d"); $yesterday = date("Y-m-d",mktime(0,0,0,$month,$day-1,$year)).":".date("Y-m-d",mktime(0,0,0,$month,$day-1,$year)); $week = date("Y-m-d",mktime(0,0,0,$month,$day-6,$year)).":".date("Y-m-d",mktime(0,0,0,$month,$day,$year)); $thismonth = date("Y-m-d",mktime(0,0,0,$month,1,$year)).":".date("Y-m-d",mktime(0,0,0,$month,$day,$year)); $lastmonth = date("Y-m-d",mktime(0,0,0,$month-1,1,$year)).":".date("Y-m-d",mktime(0,0,0,$month-1,date("t",mktime(0,0,0,$month-1,1,$year)),$year)); $thisyear = date("Y-m-d",mktime(0,0,0,1,1,$year)).":".date("Y-m-d",mktime(0,0,0,$month,$day,$year)); $lastyear = date("Y-m-d",mktime(0,0,0,1,1,$year-1)).":".date("Y-m-d",mktime(0,0,0,12,31,$year-1)); $alltime = date("Y-m-d",mktime(0,0,0,1,1,0000)).":".date("Y-m-d",mktime(0,0,0,$month,$day,$today)); ?> Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604691 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 I would do it like this: <?php function prev_month ( $now = FALSE ) { if ( !is_numeric($now) ) $now = time(); # Limit to a single date call to grab our info ( assuming parsing a string is faster than date function ) $data = date( 'n Y j t', $now ); list( $currMonth, $year, $currDay, $currDaysPerMonth ) = explode( ' ', $data ); # Find out the previous month $prevMonth = $currMonth == 1 ? 12 : $currMonth - 1; # Grab the number of days in previous month $prevDaysPerMonth = date( 't', mktime(0,0,0, $prevMonth, 1, 2008) ); # See if today is greater than or the same as the total days in previous month if ( $currDay >= $prevDaysPerMonth ) return mktime( 0,0,0, $prevMonth, $prevDaysPerMonth, ($prevMonth == 12 ? $year - 1 : $year) ); # Should be fine from here return mktime( 0,0,0, $prevMonth, $currDay, ($prevMonth == 12 ? $year - 1 : $year) ); } # Use it! echo date( 'Y-m-d', prev_month() ); ?> Returns 2008-06-30 Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604715 Share on other sites More sharing options...
Zergman Posted July 31, 2008 Author Share Posted July 31, 2008 Awesome stuff, thanks all for the help. Just reading the bug report, what does this mean "first day of +1 month" or "first day of next month" or even "last day of next month" - those are always safe to use with just a "m" or another month date format specifier. Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604755 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 They're simply saying those strtotime arguments won't bug out if you only need to calculate the month. "m" is referring to the date function.. m - Numeric representation of a month, with leading zeros - 01 through 12 Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604773 Share on other sites More sharing options...
Zergman Posted July 31, 2008 Author Share Posted July 31, 2008 They're simply saying those strtotime arguments won't bug out if you only need to calculate the month. "m" is referring to the date function.. m - Numeric representation of a month, with leading zeros - 01 through 12 Thanks for the info! I tried the code you suggested, but it errored on me and im too newbish to figure out whats wrong How can I use those simple strtotime arguments to just go back one month? Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604780 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 How did it error out? My function should run on its own and return a timestamp, just like strtotime. Are you only concerned about the month? Or did you want to find the entire date (day and year as well) 1 month ago? Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604788 Share on other sites More sharing options...
Zergman Posted July 31, 2008 Author Share Posted July 31, 2008 Basically im setting the date to use both echo on the screen and use in a sql query. Sorry about your code, copy/paste failed me that time. It did work great, but unsure how I can use it in my sql query. EDIT: Just noticed too that your code is still putting out 2008-06-30. Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604794 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 2008-06-30 is exactly 1 month ago... assuming that you wanted to find the LAST DAY of the last month, because 2008-06-31 doesn't exist And my function will return a timestamp of the day 1 month ago ( if the date doesn't exist in the previous month it will default to the last day of that month ) so simply use it in your date() function in the exact same place you used strtotime(" -1 month ") before. Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604822 Share on other sites More sharing options...
Zergman Posted July 31, 2008 Author Share Posted July 31, 2008 2008-06-30 is exactly 1 month ago... assuming that you wanted to find the LAST DAY of the last month, because 2008-06-31 doesn't exist LMAO, totally didn't figure that out till you mentioned it. .... unless there WAS 31 days in June And my function will return a timestamp of the day 1 month ago ( if the date doesn't exist in the previous month it will default to the last day of that month ) so simply use it in your date() function in the exact same place you used strtotime(" -1 month ") before. So your saying I just use your code in the $lastmonth variable I set in place of the "-1 month" ? Sorry for sounding too newbish Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604864 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 $date = date( 'Y-m-d' ); $backmonth = date( 'Y-m-d', prev_month() ); Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604875 Share on other sites More sharing options...
Zergman Posted July 31, 2008 Author Share Posted July 31, 2008 $date = date( 'Y-m-d' ); $backmonth = date( 'Y-m-d', prev_month() ); You rock, thanks discomatt, working like a charm now! Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604877 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 Glad I could help. Wanna mark the topic as 'Solved' ? Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604879 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 D'oh. I found a flaw in my function. Lemme fix it up for you Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604882 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 <?php function prev_month ( $now = FALSE ) { if ( !is_numeric($now) ) $now = time(); # Limit to a single date call to grab our info ( assuming parsing a string is faster than date function ) $data = date( 'n Y j t', $now ); list( $currMonth, $year, $currDay, $currDaysPerMonth ) = explode( ' ', $data ); # Find out the previous month $prevMonth = $currMonth == 1 ? 12 : $currMonth - 1; # Find out previous year $year = $prevMonth == 12 ? $year - 1 : $year; # Grab the number of days in previous month $prevDaysPerMonth = date( 't', mktime(0,0,0, $prevMonth, 1, $year) ); # See if today is greater than or the same as the total days in previous month if ( $currDay >= $prevDaysPerMonth ) return mktime( 0,0,0, $prevMonth, $prevDaysPerMonth, $year ); # Should be fine from here return mktime( 0,0,0, $prevMonth, $currDay, $year ); } # Use it! echo date( 'Y-m-d', prev_month() ); ?> There we go! I made the stupid assumption that the days in a month doesn't change depending on the year... then I remembered that damn February The script should compensate for leap years now. Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-604888 Share on other sites More sharing options...
Zergman Posted August 1, 2008 Author Share Posted August 1, 2008 wicked, thanks for the update! This is why I like this community. Full of helpful people that are proud of their work and are willing to tolerate newbs like me Marking as solved! Link to comment https://forums.phpfreaks.com/topic/117566-solved-date-acting-very-weird/#findComment-605132 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.