Showing posts with label time stamp. Show all posts
Showing posts with label time stamp. Show all posts

Sunday, 11 December 2016

Javascript Unix time to UTC string in Chrome console

Problem:

In the Chrome Dev Tools console, I'd like to view a Unix Time numerical value/timestamp as a human-readable UTC date/time string.

Solution:

In the Chrome Dev Tools console (and in general), one way to view a Unix (POSIX/Epoch) Time as a human-readable UTC string is to use Javascript Date.

For example, if the number of seconds since Epoch is 1480528272, we can convert this in the console by:

new Date(1480528272*1000).toUTCString()

Note that we need to convert the number of seconds to milliseconds since Epoch by multiplying by 1000.

Notes:

Note that due to the size of a Javascript integer, overflow may occur if the desired timestamp is larger than Number.MAX_SAFE_INTEGER.

A Javascript Date object also has a valid range that must be adhered to. More information can be found in here.

References:

Sunday, 22 April 2012

Time and time zones in PHP (and SQL)

Question:

How do I store, and then show the proper times at different time zones with php and SQL?

Solution:

Working with timestamps and time zones can be initially confusing with PHP. A common, but misguided solution when storing times in a database involves offseting a time by a number of hours, depending on what time zone you are in. This will lead to confusion later.
The best way to go about times and time zones is to use Unix time stamps. These are always measured in time passed since midnight, January 1, 1970 GMT, regardless of where you are in the world.
For instance, a time stamp of 1335092594 will be Sun, 22 Apr 2012 04:03:14 -0700 in PST (e.g. Seattle, WA), Sun, 22 Apr 2012 11:03:14 +0000 in UTC (e.g. Grenwich, United Kingdom), and Sun, 22 Apr 2012 21:03:14 +1000 in Sydney, Australia.

Example:

  1. To get the current time with PHP, you can use the time() function
  2. To get the date of your server, use date()
  3. To get the date at UTC, use gmdate()
  4. To change timezones in order to render that zone's local time, use date_default_timezone_set() followed by date()
e.g.:
$timestamp_now = time();
echo ' My Server Time: ' . date('r', $timestamp_now) . PHP_EOL;
echo ' UTC: ' . gmdate('r', $timestamp_now) . PHP_EOL;
date_default_timezone_set('Australia/Sydney');
echo 'Sydney, Australia: ' . date('r', $timestamp_now) . PHP_EOL;

This should yield (at a time stamp of 1335092594):
My Server Time: Sun, 22 Apr 2012 04:03:14 -0700
UTC: Sun, 22 Apr 2012 11:03:14 +0000
Sydney, Australia: Sun, 22 Apr 2012 21:03:14 +1000

Using SQL, you'll want to simply save this time stamp whenever you want to record a time, regardless of where in the world you are. A caveat is that you will want to make sure that your server is set to the correct time zone so it records the proper time stamp to begin with.

Update:

Just found a really good blog post related to this topic (PHP time and SQL). Enjoy :)
http://www.richardlord.net/blog/dates-in-php-and-mysql