Monday, 15 April 2013

php - Getting a different date on formatting -



php - Getting a different date on formatting -

i formatting date on php using next code:

$date = "2014-11-01"; $date_formatted = gmdate("y-m-d", strtotime($date));

when print them, formatted date goes day back. quite surprised. not looking solution (there many simple stuff this). i know how happened.

echo $date . " ---> " . $date_formatted; //displays: 2014-11-01 ---> 2014-10-31

it's because utilize gmdate. give date in gmt based on supplied timestamp.

strtotime not utilize gmt default , instead utilize local timezone. when phone call gmdate, cut down date , time timezone offset.

to prepare this, utilize date instead of gmdate or set system's timezone utc.

php

No comments:

Post a Comment