Home  >  Article  >  Backend Development  >  How to use php timestamp

How to use php timestamp

小云云
小云云Original
2018-03-16 14:23:113065browse

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[&#39;type&#39;];
switch ($type) {
    case 3: { // 本月
        $start = mktime(0, 0, 0, date(&#39;m&#39;), 1, date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 1, date(&#39;Y&#39;));
    };
        break;
    case 6: { //上月
        $start = mktime(0, 0, 0, date(&#39;m&#39;) - 1, 1, date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), 1, date(&#39;Y&#39;)) - 1;
    };
        break;
    case 7: { //本周
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - date(&#39;w&#39;), date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;));
    };
        break;
    case 8: { //上周
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 7 - date(&#39;w&#39;), date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - date(&#39;w&#39;), date(&#39;Y&#39;)) - 1;
    };
        break;
    case 4: { // 本年
        $start = mktime(0, 0, 0, 1, 1, date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, 1, 1, date(&#39;Y&#39;) + 1);
    };
        break;
    case 5: { // 昨天
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 1, date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;)) - 1;
    };
        break;
    case 9: { // 前七天
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 6, date(&#39;Y&#39;));
        $end = mktime(date(&#39;H&#39;), date(&#39;m&#39;), date(&#39;s&#39;), date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;));
    };
        break;
    case 2: { // 前30天
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 29, date(&#39;Y&#39;));
        $end = mktime(date(&#39;H&#39;), date(&#39;m&#39;), date(&#39;s&#39;), date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;));
    };
        break;
    case 1: { // 今天
        $start = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;));
        $end = mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 1, date(&#39;Y&#39;)) - 1;
    };
        break;
    default: {
        return &#39;&#39;;
    }
}

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn