Tag Archives: timestamp

PHP: How to Convert a Timestamp into a Date String Programming 26 MAR 2015

Unix timestamps aren’t exactly human readable representations of time. After all, a Unix timestamp is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Usually one wants to show this to a user in a much friendlier date time format, like the tried and trusted Y-m-d H:i:s standard, e.g. ‘2015-03-23 09:00’.

It turns out achieving this formatted display is relatively easy: just use the standard DateTime class!

You can use a timestamp as construct parameter if you add the @-sign at the front. Once that is done, simply use the format function to return a user friendly date/time string:

$dateTimeEnd = new DateTime('@1427105451'); 
echo $dateTimeEnd->format('Y-m-d H:i:s'); //prints 2015-03-23 10:10:51

Quick and simple.


Related Link: http://php.net/manual/en/class.datetime.php

MySQL: How add an Hour to a DateTime Column Value CodeUnit 29 AUG 2011

If you make use of a datetime column in your MySQL database to store timestamp data, here is a simple way of adding a couple of hours to your already stored value. You know, if the client suddenly decided to change time zone or something like that! ;)

UPDATE `table` SET `datetime-column` = DATE_ADD(`datetime-column`, INTERVAL 2 HOUR) WHERE `id` = 1

You can of course also minus hours by altering the above to add a negative interval:

UPDATE `table` SET `datetime-column` = DATE_ADD(`datetime-column`, INTERVAL -2 HOUR) WHERE `id` = 1

Simple and effective.

PHP: Determine the Offset between two Time Zones Programming 07 SEP 2009

Because I keep forgetting where I put this code fragment on my system, grabbed from the comments section from the PHP online manual just by the way, I may as well just pop it on here so that I can keep track of it better in the future.

The function you see below is useful for determining the offset (in seconds) between your local (origin) timezone and a remote timezone. In order to run, it requires a remote timezone region string, as well as a second timezone region string if you don’t wish to use the system’s preset timezone. (And if you’re looking for timezone regions strings, may I suggest the excellent http://www.worldtimezones.com as a source?)

So, without further ado, here’s the nifty code:

/**    Returns the offset from the origin timezone to the remote timezone, in seconds.
*    @param $remote_tz;
*    @param $origin_tz; If null the servers current timezone is used as the origin.
*    @return int;

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if($origin_tz === null) {
        if(!is_string($origin_tz = date_default_timezone_get())) {
            return false; // A UTC timestamp was returned -- bail out!
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    return $offset;

And what good would the code be without a nice example to go along with it:

// This will return 10800 (3 hours) ...
$offset = get_timezone_offset('America/Los_Angeles','America/New_York');
// or, if your server time is already set to 'America/New_York'...
$offset = get_timezone_offset('America/Los_Angeles');
// You can then take $offset and adjust your timestamp.
$offset_time = time() + $offset;

It’s clean, it works and I like it, so kudos to the person named Dan at authenticdesign.net then! :)

wall clocks for different time zones