Jump to content

[SOLVED] date() acting very weird


Zergman

Recommended Posts

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

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

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));
?>

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

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.

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?

 

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.

 

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.

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

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.