Unix timestamp and date conversion
Unix timestamp is a method of displaying the time by calculating 0:0:0 seconds as 0 on January 1, 1970.
Each second passes, it increases by 1, so 0:0:1 on January 1, 1970 becomes 1.
For another example, on January 1, 1970, 0:01:0 is 60.
(60 seconds~ for 1 minute)
To display the current time as Unix timestamp in php, use the time() function.
$timestamp = time();
And to display the specified date and time in Unix timestamp, use the strtotime() function.
$timestamp = strtotime("2015-08-03 13:42:00");
If the current time 2015-08-03 13:42:00 is expressed as Unix timestamp, it is 1438531192. That is, the current time is 1438531192 seconds from 0:0:0 on January 1, 1970.
However, it is said that 0:0:0 on January 1, 1970 is based on global standard time (Greenwich Observatory, UK), not Korea.
(9 hours faster than Korea.)
If an unexpected value comes out while coding and there is a difference of approximately 9 hours, it is most likely due to the time difference from Universal Time.
When converting a Unix timestamp to a date string, use the date function. If you want to change it to universal time, use the gmdate function.
(Refer to php sample code and output result)
// 현재 시간을 Unix timestamp로 변환한다. $timestamp = time(); echo "날짜1 : "; echo $timestamp; echo "<br />"; // 특정 날짜를 Unix timestamp로 변환한다. $timestamp = strtotime("2015-08-03 13:42:00"); echo "날짜2 : "; echo $timestamp; echo "<br />"; // date를 이용한 날짜 출력 echo "날짜3 : "; echo date("Y-m-d h:i:s", $timestamp); echo "<br />"; // gmdate를 이용한 날짜 출력 echo "날짜4 : "; echo gmdate("Y-m-d h:i:s", $timestamp); echo "<br />";
날짜1 : 1618752276 날짜2 : 1438576920 날짜3 : 2015-08-03 13:42:00 날짜4 : 2015-08-03 04:42:00
Date 1 is the output of the current time as Unix timestamp, so the value changes every time the page is executed.
Date 2 is the output value of 13:42:00 on August 03, 2015 as Unix timestamp.
Date 3 is the Unix timestamp value converted to a date string.
The output is the same as 13:42:00 on August 03, 2015, which was used in Date 2.
Date 4 is the Unix timestamp value changed to the universal time string.
Because Universal Time is 9 hours ahead, it is output as 4:42.