Unix timestamp is the number of seconds since january 1, 1970 00:00:00 UTC. PHP strtotime() and date() functions are used to get unix timestamp and human readable date respectively. Setting different timezones while converting date or timestamp differs the final results.
Using strtotime() function we can get unix timestamp. it is also very important to note that strtotime() requires a default timezone otherwise it will generate timestamp in server timezone which could be misleading.
<?php
// set default timezone
date_default_timezone_set('America/Los_Angeles');
//define date and time
$date = date("d M Y H:i:s");
// output
echo strtotime($date);
?>
We can use date() function to generate readable date.
<?php
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
?>
MySQL | SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2072 12:00AM', '%M %d %Y %h:%i%p')) |
SQLite | SELECT strftime('%s', 'now') |
PostgreSQL | SELECT to_timestamp('20/8/2072 14:52:49', 'DD/MM/YYYY hh24:mi:ss')::timestamp |
Go | time.Now().Unix() |
JavaScript | Math.floor(new Date().getTime()/1000.0) |
Lua | epoch = os.time([date]) |
Java | long epoch = System.currentTimeMillis()/1000 |
Ruby | str = "2072-05-27T07:39:59Z" Time.parse(str).to_i |
MySQL | select DATE_FORMAT(FROM_UNIXTIME(your_timestamp_col), '%e %b %Y') AS date FROM your_table |
SQLite | SELECT strftime('%d - %m - %Y ', datetime(your_timestamp_col, 'unixepoch')) FROM your_table; |
PostgreSQL | select to_timestamp(epoch_column)::date |
Go | currentSeconds := time.Now().Unix() fmt.Println(“Time in seconds: “, currentSeconds) |
JavaScript | new Date (timestamp); |
Lua | local ts = os.time() print(os.date('%Y-%m-%d %H:%M:%S', ts)) |
Java | Timestamp stamp = new Timestamp(System.currentTimeMillis()); Date date = new Date(stamp.getTime()); System.out.println(date); |
Ruby | Time.at(timestamp) |