This article mainly shares with you how to use php timestamp. This article mainly shares it with you in the form of code. I hope it can help you.
$date = date('Y-m-d',time());//今日"2018-3-1" //php获取今日开始时间戳和结束时间戳 $start = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1; 将英文文本日期时间解析为 Unix 时间戳: echo(strtotime("now") . "<br>"); echo(strtotime("15 October 1980") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday"));
$type = $param['type']; switch ($type) { case 3: { // 本月 $start = mktime(0, 0, 0, date('m'), 1, date('Y')); $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); }; break; case 6: { //上月 $start = mktime(0, 0, 0, date('m') - 1, 1, date('Y')); $end = mktime(0, 0, 0, date('m'), 1, date('Y')) - 1; }; break; case 7: { //本周 $start = mktime(0, 0, 0, date('m'), date('d') - date('w'), date('Y')); $end = mktime(0, 0, 0, date('m'), date('d'), date('Y')); }; break; case 8: { //上周 $start = mktime(0, 0, 0, date('m'), date('d') - 7 - date('w'), date('Y')); $end = mktime(0, 0, 0, date('m'), date('d') - date('w'), date('Y')) - 1; }; break; case 4: { // 本年 $start = mktime(0, 0, 0, 1, 1, date('Y')); $end = mktime(0, 0, 0, 1, 1, date('Y') + 1); }; break; case 5: { // 昨天 $start = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); $end = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1; }; break; case 9: { // 前七天 $start = mktime(0, 0, 0, date('m'), date('d') - 6, date('Y')); $end = mktime(date('H'), date('m'), date('s'), date('m'), date('d'), date('Y')); }; break; case 2: { // 前30天 $start = mktime(0, 0, 0, date('m'), date('d') - 29, date('Y')); $end = mktime(date('H'), date('m'), date('s'), date('m'), date('d'), date('Y')); }; break; case 1: { // 今天 $start = mktime(0, 0, 0, date('m'), date('d'), date('Y')); $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1; }; break; default: { return ''; } }
Pass $end and $start as where conditions and use
Using the date() method can only get the year, month and day of the current time, but when you query from the database The time format cannot be obtained using this method, such as: date("d",$time);
But using date(), you can obtain date("Y",$time), date("M ",$time),date("D",$time), but if we want to get the current date, we can use the split function
twice and that's it!
<?php //取得时间的年 $strtimes=$info[datetime] $strtimes = explode(" ",$strtime); $timearray = explode("-",$strtimes[0]); $year = $timearray[0]; $month = $timearray[1]; $day = $timearray[2]; ?>
Related recommendations:
PHP timestamp and date conversion example sharing
php timestamp function usage summary
PHP timestamp usage example code_PHP tutorial
The above is the detailed content of How to use php timestamp. For more information, please follow other related articles on the PHP Chinese website!