php - gmdate not constant across servers? -


i've been working on application needs lot of calculations time.

to keep working supposed to, started with:

$datetoday = strtotime(gmdate("m/d/y")); 

as understood, supposed return gmt date. whichever timezone server in, should same time?

so, developed on localhost worked smoothly, when moved server in different timezone, started getting problems.

echo'ing "$datetoday" on localhost gives me "1501192800", while doing same on server "1501214400". same 6h time difference between local machine , server.

so missing here?

thanks

$datetoday = strtotime(gmdate("m/d/y")); 

as understood, supposed return gmt date.

not @ all.

gmdate() formats date using gmt timezone. since called 1 argument, gets date format calling time().

time() returns current timestamp, i.e. number of seconds passed since jan 1, 1970, 0:00:00 utc.

then, gmdate() uses format provide in first argument produce text representation of second argument (the current timestamp saw). value returned gmdate("m/d/y") 07/28/2017.

then, 07/28/2017 passed strtotime() tries interpret date , computes timestamp it. because doesn't contain time component, strtotime() assumes time 0:00:00.

and here comes catch. because provided string not contain timezone information, php uses configuration assume timezone. discovered, running code on 2 servers can produce different results because of different configurations.

for me, strtotime(gmdate('m/d/y')) returns 1501200000 , timestamp (the moment in time) when today (july 18, 2017) started on utc timezone (because php used testing uses "utc" default timezone). values on different servers time moments when today started on respective timezones.

if run same code php correctly configured timezone (europe/bucharest) 1501189200. 10800 seconds before value use utc because europe/bucharest on utc+3 (the regular +2 of eastern europe standard time plus 1 hour because of daylight savings time.

php provides set of classes handle date , time: datetime , other classes name start date (find them in documentation). easier use old date & time functions , know how handle timezones (the old functions not know).

creating datetime object stores start of today in utc timezone easy as:

$datetoday = new datetime("today 0:00:00", new datetimezone("utc")); 

you can create datetime object "now" (in desired timezone) , set time 0:00:00:

$datetoday = new datetime("now", new datetimezone("utc")); $datetoday->settime(0, 0, 0); 

for datetime objects not modified in code (they read) better create datetimeimmutable objects. class prevents accidental changing of values stores.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -