PHP Local Time

I learn something new every day as far as PHP is concerned.

The PHP manual clearly states how to obtain a past or future date and time using the date() and mktime() functions. What it doesn't state is how to easily convert between two different time zones. I had to figure it out on my own and guess what? It was a heck of a lot easer than I thought it would be. I didn't need any form of reference other than the PHP manual itself.

Here's the code:

$timezone = "Asia/Manila";
putenv("TZ=".$timezone); // PHP4
date_default_timezone_set('Asia/Manila'); // PHP5
$date = date("Ymd H:i:s", mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"), date("I")));

Before I tested this code, I did not know that putenv() would affect the remaining date and time functions in the script. It was nothing more than a lucky guess.

Similar Posts:

4 Comments

  1. mikx says:

    In PHP 5 better to use date_default_timezone_set()

    http://www.php.net/manual/en/function.date-default-timezone-set.php

    When using strftime() do not forget to set setlocale(LC_TIME, "zone_ZONE") accordingly.

    http://www.php.net/manual/en/function.setlocale.php

  2. Thomas says:

    This mktime always looks so damn ugly. I prefer using time().

  3. You could do it with time, but the point was putenv(). They never tell you everything you need to know.

  4. Carlo says:

    Thanks for posting. it works and it helps.. :D

Leave a Reply

CommentLuv Enabled

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.

Anti-Spam Protection by WP-SpamFree