I’m having a strange problem while trying to make a date from some user input using gmmktime(). It seems to always show my date as being one day behind when I convert it to a date for output. I have a date selection, and I am converting the date using the input from a month, year, and day drop down picker.
In my code, I have:
$inputDate = gmmktime(0,0,0,02,07,2012,0);
$inputDate = date("M d Y", $inputDate);
When this is output, the date that shows is Feb 06, 2012 instead of Feb 07, 2012. However if I change the gmmktime() to just mktime(), the date is correct and shows as the 7th.
Can someone explain why this is happening and what I need to do in order to use gmmktime() and have my dates displayed in the right format? Thanks!
The
gmmktime()assumes that the entered date/time is GMT and automatically converts it to server time using date.timezone setting in php.ini. On my system which is +0500 GMT I get:Notice that I passed
0, 0, 0as the time to the function but ended up getting05:00:00. In your case it seems like your server is behind GMT so for every date you create using this function will be converted to -nnnn hours behind the specified date; for 00:00 GMT as the input this effectively means you get the previous date.The workaround is simple, use
gmdate()function to display dates created withgmmktime():