Skip to content Skip to sidebar Skip to footer

How To Show A Unix-time In A Local Time Format

I have a php variable say $expTime (which has a unixtime say-1359683953) . I want to display this variable on the client side(in a proper time format according to his local time) .

Solution 1:

Use DateTime with timezones:

$datetime = new DateTime('@1359683953');
echo$datetime->format('h:i M d/y') . "<br>";
$datetime->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo$datetime->format('h:i M d/y');

See it in action

Solution 2:

UNIX time values are usually UTC seconds since epoch. Javascript time values are UTC milliseconds since the same epoch. The ECMA-262 Date.prototype.toString method automatically generates a string representing the local time and takes account of daylight saving if it applies.

You can also use Date methods to create your own formatted string, they also return local time and date values. There are also UTC methods if you want to work in UTC.

To do this on the client, just provide a UTC time value from the server and then all you need in javascript is:

var timeValue = 1359683953;  // assume secondsvar date = newDate(timeValue * 1000);  // convert time value to millisecondsalert(date); //  Fri 01 Feb 2013 11:59:13 GMT+1000 

Post a Comment for "How To Show A Unix-time In A Local Time Format"