PDA

View Full Version : php mktime question


Vitesse
01-04-2007, 11:48 PM
Hi there,

Im doing something to display when a monthly newsletter will be released which is always the 1st of the month, so i've setup a plugin that i'm intending to use to put the date into a variable $nextmonth.

Problem is i cant seem to get the correct value.

This is what i've got at the minute:


$nextmonth = date("d/m/Y", mktime(0, 0, 0, 1, m+1, Y));

I know this doesnt work, as it always returns the value 01/01/2000 for some reason.

Effectively what i'm trying to achieve is that $nextmonth will currently have the value 01/02/2007 and if the date were say february now then it will be 01/03/2007 i.e. day will always be 01 month will be the next month and year will be next year if month is december. I've looked through lots of examples of this but cant really work out how to adapt it to what i need.

Anyone got any idea how to do this?

noppid
01-05-2007, 03:32 PM
Try this...


$nextmonth = date("d/m/Y", mktime(0, 0, 0, 1, date("m")+1, date("Y")));


the date function returns a number you can do the math on.

Vitesse
01-05-2007, 05:43 PM
Thanks Noppid, the month seems to be doing what its supposed to but the date is coming out as 02/01/2007 rather than 01/02/2007

Any ideas?

Adrian Schneider
01-05-2007, 06:47 PM
$nextmonth = date('d/m/Y', mktime(0, 0, 0, date('m') + 1, 1, date('Y')));

mktime uses month day year instead of the expected day month year

noppid
01-05-2007, 07:01 PM
Thanks Noppid, the month seems to be doing what its supposed to but the date is coming out as 02/01/2007 rather than 01/02/2007

Any ideas?

Simple adjustment.


$nextmonth = date("m/d/Y", mktime(0, 0, 0, 1, date("m")+1, date("Y")));


We just moved the m and d in the output format.

Vitesse
01-06-2007, 05:50 AM
Simple adjustment.


$nextmonth = date("m/d/Y", mktime(0, 0, 0, 1, date("m")+1, date("Y")));


We just moved the m and d in the output format.

Fantasic, works perfectly, thanks Noppid & sirAdrian

got one question with regards to this, would it carry the year over if say for example current date was mid december would it then correctly display 01/01/08 ?

Dismounted
01-06-2007, 08:52 AM
From the looks of it, no, because the year is not programmed to be one year ahead in your specified circumstances.

Antivirus
01-07-2007, 01:05 AM
also, FYI if you were concerned with including a timestamp as well as the date, you would want to use the vbmktime function to ensure that the timestamp were in-synch with the recipient's selected timezone in their profile.

Adrian Schneider
01-07-2007, 01:33 AM
Yep, vbdate too...

BTW not sure if you applied what I said in my post or not, but if you didn't your results will be incorrect next month.

Vitesse
01-07-2007, 07:02 AM
From the looks of it, no, because the year is not programmed to be one year ahead in your specified circumstances.


How would that need to be altered to take the year into account?


Yep, vbdate too...

BTW not sure if you applied what I said in my post or not, but if you didn't your results will be incorrect next month


It that simply a case of changing date to be vbdate or does the function word differently?

Dismounted
01-07-2007, 08:28 AM
http://members.vbulletin.com/api/vBulletin/_includes_functions_php.html#functionvbdate

Read up on the function 'vbdate' :).