Detailed explanation of php getting time

墨辰丷
Release: 2023-03-25 18:10:01
Original
1694 people have browsed it

This article mainly shares with you PHP to get the time, date() formats a local time/date, and returns a string generated by integer timestamp according to the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional, and the default value is time()

<?php  
/** 
 * php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内) 
 * 
 * author:ihelloworld2010@gmail.com 
 * date:2012-06-28 16:00:01 
 */  
  
$q = $_GET[&#39;q&#39;] ? intval($_GET[&#39;q&#39;]) : 0;  
  
$text = &#39;&#39;;  
$now = time();  
  
if ($q === 1) {// 今天  
    $text = &#39;今天&#39;;  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, $now);  
    $endTime = date(&#39;Y-m-d 23:59:59&#39;, $now);  
} elseif ($q === 2) {// 昨天  
    $text = &#39;昨天&#39;;  
    $time = strtotime(&#39;-1 day&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, $time);  
    $endTime = date(&#39;Y-m-d 23:59:59&#39;, $now);  
} elseif ($q === 3) {// 三天内  
    $text = &#39;三天内&#39;;  
    $time = strtotime(&#39;-2 day&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, $time);  
    $endTime = date(&#39;Y-m-d 23:59:59&#39;, $now);  
} elseif ($q === 4) {// 本周  
    $text = &#39;本周&#39;;  
    $time = &#39;1&#39; == date(&#39;w&#39;) ? strtotime(&#39;Monday&#39;, $now) : strtotime(&#39;last Monday&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, $time);  
    $endTime = date(&#39;Y-m-d 23:59:59&#39;, strtotime(&#39;Sunday&#39;, $now));  
} elseif ($q === 5) {// 上周  
    $text = &#39;上周&#39;;  
    // 本周一  
    $thisMonday = &#39;1&#39; == date(&#39;w&#39;) ? strtotime(&#39;Monday&#39;, $now) : strtotime(&#39;last Monday&#39;, $now);  
    // 上周一  
    $lastMonday = strtotime(&#39;-7 days&#39;, $thisMonday);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, $lastMonday);  
    $endTime = date(&#39;Y-m-d 23:59:59&#39;, strtotime(&#39;last sunday&#39;, $now));  
} elseif ($q === 6) {// 本月  
    $text = &#39;本月&#39;;  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, mktime(0, 0, 0, date(&#39;m&#39;, $now), &#39;1&#39;, date(&#39;Y&#39;, $now)));  
    $endTime = date(&#39;Y-m-d 23:39:59&#39;, mktime(0, 0, 0, date(&#39;m&#39;, $now), date(&#39;t&#39;, $now), date(&#39;Y&#39;, $now)));  
} elseif ($q === 7) {// 三月内  
    $text = &#39;三月内&#39;;  
    $time = strtotime(&#39;-2 month&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, mktime(0, 0,0, date(&#39;m&#39;, $time), 1, date(&#39;Y&#39;, $time)));  
    $endTime = date(&#39;Y-m-d 23:39:59&#39;, mktime(0, 0, 0, date(&#39;m&#39;, $now), date(&#39;t&#39;, $now), date(&#39;Y&#39;, $now)));  
} elseif ($q === 8) {// 半年内  
    $text = &#39;半年内&#39;;  
    $time = strtotime(&#39;-5 month&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, mktime(0, 0,0, date(&#39;m&#39;, $time), 1, date(&#39;Y&#39;, $time)));  
    $endTime = date(&#39;Y-m-d 23:39:59&#39;, mktime(0, 0, 0, date(&#39;m&#39;, $now), date(&#39;t&#39;, $now), date(&#39;Y&#39;, $now)));  
}  elseif ($q === 9) {// 一年内  
    $text = &#39;一年内&#39;;  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, mktime(0, 0,0, 1, 1, date(&#39;Y&#39;, $now)));  
    $endTime = date(&#39;Y-m-d 23:39:59&#39;, mktime(0, 0, 0, 12, 31, date(&#39;Y&#39;, $now)));  
} elseif ($q === 10) {// 三年内  
    $text = &#39;三年内&#39;;  
    $time = strtotime(&#39;-2 year&#39;, $now);  
    $beginTime = date(&#39;Y-m-d 00:00:00&#39;, mktime(0, 0, 0, 1, 1, date(&#39;Y&#39;, $time)));  
    $endTime = date(&#39;Y-m-d 23:39:59&#39;, mktime(0, 0, 0, 12, 31, date(&#39;Y&#39;)));  
}  
  
echo $text;  
echo &#39;<br />&#39;;  
echo $beginTime;  
echo &#39;<br />&#39;;  
echo $endTime;
Copy after login

Summary:

I hope that through this article, friends can learn more about php Time to have a deeper understanding and mastery.

Related recommendations:

php get time code summary sharing

PHP get time difference

Several ways to get time in php

php get time (system time and network time)

The above is the detailed content of Detailed explanation of php getting time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template