Home >Backend Development >PHP Tutorial >PHP, Mysql date and time sorting_PHP tutorial
I have gained a lot after working for a year. I will slowly make some summaries to improve work efficiency.
The time used by mysql at work is a UNIX timestamp: the number of seconds from 0:00 on January 1, 1970 to the current time. Since it is an int type, it is very convenient for computer processing, not just PHP and MySQL is a format for data interaction. It is also the standard for data interaction in various clients (android/IOS), etc. Therefore, if you only save and display dates, you should use UNIX timestamps to calculate dates and as standard Date format.
The commonly used process in work is: convert the time of the HTML page into a timestamp and save it in mysql, and then take the timestamp from mysql and format it for display on the web or mobile client. In short, the time saved in mysql is a UNIX timestamp, which is then formatted into a suitable time by PHP
Introducing several commonly used functions
1.date(),2.mktime(),3.getdate(),4.strftime()
1.date()
Get time and date in PHP
Use the date() function: convert the timestamp or current time into a formatted string, for example:
echo date('Y-i-s');//Output 2014-3-25
2.mktime()
Use mktime() to convert time into UNIX timestamp
$timestamp = mktime();
There are three ways to get the current timestamp:
mktime(),time(),date('U')
mktime does time calculations
mktime(12,0,0,$mon,$day+10,$year); timestamp ten days later
3.getdate() function:
$today = getdate();
print_r($today);
//Output
Array
(
[seconds] => 38
[minutes] => 38
[hours] => 22
[mday] => 25
[wday] => 2
[mon] => 3
[year] => 2014
[yday] => 83
[weekday] => Tuesday
[month] => March
[0] => 1395758318
)
Use checkdate() function to check date validity
4.strftime()
Format timestamp
mysql formatting time
1.DATE_FORMAT()
2.UNIX_TIMESTAMP() returns the date formatted as a UNIX timestamp, for example: SELECT UNIX_TIMESTAMP(date) FROM table, so it can be processed in PHP
There are relatively few functions for formatting time in PHP. Here are some commonly used formatting time functions
/** * *将timestamp时间转化为x时x分x秒 * */ public static function getTimeLong($seconds) { if (!$seconds) { return '0秒'; } $ret = ''; if ($seconds >= 3600) { $hours = (int)($seconds / 3600); $seconds = $seconds % 3600; if ($hours) { $ret .= ($hours . '时'); } } if ($seconds >= 60) { $mi = (int)($seconds / 60); $seconds = $seconds % 60; if ($mi) { $ret .= ($mi . '分'); } } if ($seconds) { $ret .= ($seconds . '秒'); } return $ret; }
/** * 将相差timestamp转为如“1分钟前”,“3天前”等形式 * * @param timestamp $ts_diff 当前时间 - 要格式化的timestamp */ public static function formatTime($ts_diff) { if ($ts_diff <=0) { return date('Y-m-d'); } else if ( $ts_diff <= 3600 ) { return max(1, (int)($ts_diff/60)) . '分钟前'; } else if ( $ts_diff <= 86400 ) { return ((int)($ts_diff/3600)) . '小时前'; } else { return ((int)($ts_diff/86400)) . '天前'; } }
/** 将数字星期转换成字符串星期 weekNum2String($num) * @param int * @return string */ public static function weekNum2String($num){ switch($num){ case 1: return '星期一'; case 2: return '星期二'; case 3: return '星期三'; case 4: return '星期四'; case 5: return '星期五'; case 6: return '星期六'; case 7: return '星期日'; default: return '未知'; } }