0

i recently tried setting the timezone to Singapore. In my computer's time, it shows 3AM but when i tried echoing the one that i set on PHP, the time is 10:51:00 (Which is like 7 hours later) What can i do?

Below is the code that i had tried:

date_default_timezone_set("Asia/Singapore");
echo date('Y-m-d H:i:s');

for now, i dont really care if i set it to other neighbouring countries.. but i just want the GMT to be +8 so that i can use the Now() function for the datetime so as i could insert it into my database.. thanks!

ZQ7
  • 97
  • 11

1 Answers1

1

Welcome to the wonderfully confusing (even to those of us doing this for years) world of 'time'!

From my experience, if you are going to work with PHP/MySQL/etc. for very long, you will run into this 'issue' over and over. I will, therefore, give you the best advice I ever got about this - "whenever you are working with time, learn to work with and set everything to GMT on the server side - change it to the User's timezone when you display it." This way, you will save yourself a lot of stress as you work with users all over the world (and, if you stay in programming for long, you will!)

Now, why does your echo date() not output what you think it should? See http://php.net/manual/en/function.date.php which states

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

Note that "local time" means the time on the server where your php file is running - so, if it isn't in your local area, you won't get what you expect.....

Want to see what timezone your server is in?

$date = new DateTime();    
$tz = $date->getTimezone();
echo $tz->getName();

To get the time you want from PHP:

$date = new DateTime(null, new DateTimeZone('Asia/Singapore'));
echo $date->format('Y-m-d H:i:s');

You also had tried date_default_timezone_set....

date_default_timezone_set is a PHP function, though your question says you want to use 'NOW()' in a database......

To set the database time, you need to use a command in the database - see this question for several examples of how that is done.

So, your question is quite multi-tiered (and typically won't get answered), though I know the confusion of just what you asked and I trust this response will help you get on your way to working with time.

EDIT: showing my results from a Google search of 'Singapore time' (left) and my server PHP code and result (right); enter image description here

Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28
  • This is really informative, thank you for taking the time to answer my question.. when i echo $tz->getName(); , it shows Asia/Singapore.. so the timezone is correct.. well time for me to experiment with the help you had provided :) Thanks again! – ZQ7 Jan 28 '18 at 07:40
  • one question sir, when i check the timezone, it shows that Asia/Singapore which is correct, but when i echo out the datetime, it still shows that it's 8 hours later than the actual time in Singapore.. any other help? :) – ZQ7 Jan 28 '18 at 07:50
  • That is odd - what happens when you $date = new DateTime(); echo $date->format('Y-m-d H:i:s'); That is a more common method now (object oriented style) and though I have used procedural style for years, I'm find this the 'better' way to work with time. – Apps-n-Add-Ons Jan 28 '18 at 12:08
  • it ouputs: 2018-01-30 21:11:32 But in SG time now, it is 1:21pm which is 13:21 :/ Will it work if i were to set it to other countries that has the same GMT +8 ? – ZQ7 Jan 30 '18 at 05:22
  • I updated my answer to show what I get using the code shown. This is the normal, expected behavior on any php server (I tested it on two I use and exact results, as expected). If you do not get such response, I would talk with your server provider about how they set up the server. – Apps-n-Add-Ons Jan 30 '18 at 13:24