The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don't have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().
However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.
NOTE : Timestamps in MariaDB/MYSQL have a maximum value of 2147483647, equivalent to 2038-01-19 05:14:07. This is due to the underlying 32-bit limitation. Using the function on a date beyond this will result in NULL being returned.
NOTE : Timestamps in MariaDB/MYSQL have a maximum value of 2147483647, equivalent to 2038-01-19 05:14:07. This is due to the underlying 32-bit limitation. Using the function on a date beyond this will result in NULL being returned.
So It's a good option to use other way like this simple php function.
The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.
The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.
// 1460865605 -> 2016-04-17 00:00:05
function Timestamp2Date($t = null){ if($t == null){ $t = time(); } return date('Y-m-d H:i:s', $t); }
// Any Date format to Timestamp
echo(strtotime("now") . "<br>"); // 1461001723 echo(strtotime("2016-04-17") . "<br>"); // 1460865600 echo(strtotime("2016-04-17 00:00:05") . "<br>"); // 1460865605 echo(strtotime("16 October 2016") . "<br>"); // 1476590400 echo(strtotime("+5 hours") . "<br>"); // 1461019723 echo(strtotime("+1 week") . "<br>"); // 1461606523 echo(strtotime("+1 week 3 days 7 hours 5 seconds")."<br>");//1461890928 echo(strtotime("next Monday") . "<br>"); //1461556800 echo(strtotime("last Sunday")); // 1460865600
Convert different date time format functions
function return_date($date="") {
if (!empty($date)) {
return date("d-m-Y",strtotime($date));
} else {
return "";
}
}
//
function return_time($datetime="") {
if (!empty($datetime)) {
return date("d-m-Y h:i A",strtotime($datetime));
} else {
return "";
}
}
function convert_date($date="") {
if (!empty($date)) {
return date("Y-m-d",strtotime($date));
} else {
return "";
}
}
function convert_time($datetime="") {
if (!empty($datetime)) {
return date("Y-m-d H:i:s",strtotime($datetime));
} else {
return "";
}
}
|
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).