Unix time is widely used in unix-like operatin system to track down the total seconds. PHP provides some easy way to convert between Unix Time and Human Time, let’s first look at the
date() formats a local time/date according to its parameter format.
Unix time (aka POSIX time or Epoch time), is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap second.
|Example of formats||Example of Returned values|
|jS F Y||19 May 2014|
|DATE_RFC2822||Mon, 19 May 2014 12:39:36 +0000|
Refer to the manual for more time/date format.
Human Readable Time to Unix Time
- Let’s look at the
1 2 3 4
You can get any format
For instance, we have a string of time 010000 which indicates 1 hour. We can change this into second by using
mktime() function like this:
Yes, because the Unix time start from 1st of Jan, 1970 so we can convert time into seconds like this way.
- Using DateTime object and
getTimestamp. Let’s us see what this DateTime can do. By using
createFromFormatinstance, we can convert a very complex time format into Unix Time.
1 2 3 4 5
- Of course we cannot forget this handy
strtotime. It can understand English.
1 2 3
This is pretty straight forward comparing to other 2.
This is my way to convert complex human readable time to Unix time via PHP. However, it’s important to know that some of this function might fail at some point near the year 2038.