Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

portability of php date() and time() functions

Tags:

date

php

time

cron

Its my understanding that date() and time() functions native to php will give me a timestamp or date formatted based on the local time of the location of the server. So firstly if I'm wrong about this feel free to let me know.

So if thats the case, and I needed to write for example some functions to determine what time it is for a cron job to see if it should execute, I would need to sort of customize it to a default timezone. Again, correct me if I'm wrong.

So that being said, if I did that and then changed hosting servers, i would need to rewrite the file that determined the time correct because timezone offset of gmt would now be different.

Is this the case or is there a better approach?

like image 339
Rooster Avatar asked Dec 07 '25 05:12

Rooster


1 Answers

date() and time() functions native to php will give me a timestamp or date formatted based on the local time of the location of the server

Not 100% true. time() will return the same value regardless of location, timezone, setting, etc as all unix timestamps are un UTC.

Secondly, date() will return formatted values based on PHP's timezone setting (which is usually the same as the server). See php.net/date_default_timezone_set for examples and more info. It's highly advised (even by PHP's own error reporting) that your application set the default timezone very early to avoid issues.

So that being said, if I did that and then changed hosting servers, i would need to rewrite the file that determined the time correct because timezone offset of gmt would now be different.

That's why it's so important to set date_default_timezone_set in your application. This gives it more portability so when you move servers you don't need to worry about changing it's time settings.

like image 166
Mike B Avatar answered Dec 08 '25 18:12

Mike B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!