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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.