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['q'] ? intval($_GET['q']) : 0; $text = ''; $now = time(); if ($q === 1) {// 今天 $text = '今天'; $beginTime = date('Y-m-d 00:00:00', $now); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 2) {// 昨天 $text = '昨天'; $time = strtotime('-1 day', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 3) {// 三天内 $text = '三天内'; $time = strtotime('-2 day', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', $now); } elseif ($q === 4) {// 本周 $text = '本周'; $time = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now); $beginTime = date('Y-m-d 00:00:00', $time); $endTime = date('Y-m-d 23:59:59', strtotime('Sunday', $now)); } elseif ($q === 5) {// 上周 $text = '上周'; // 本周一 $thisMonday = '1' == date('w') ? strtotime('Monday', $now) : strtotime('last Monday', $now); // 上周一 $lastMonday = strtotime('-7 days', $thisMonday); $beginTime = date('Y-m-d 00:00:00', $lastMonday); $endTime = date('Y-m-d 23:59:59', strtotime('last sunday', $now)); } elseif ($q === 6) {// 本月 $text = '本月'; $beginTime = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m', $now), '1', date('Y', $now))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 7) {// 三月内 $text = '三月内'; $time = strtotime('-2 month', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 8) {// 半年内 $text = '半年内'; $time = strtotime('-5 month', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, date('m', $time), 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, date('m', $now), date('t', $now), date('Y', $now))); } elseif ($q === 9) {// 一年内 $text = '一年内'; $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, 1, 1, date('Y', $now))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y', $now))); } elseif ($q === 10) {// 三年内 $text = '三年内'; $time = strtotime('-2 year', $now); $beginTime = date('Y-m-d 00:00:00', mktime(0, 0, 0, 1, 1, date('Y', $time))); $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y'))); } echo $text; echo '<br />'; echo $beginTime; echo '<br />'; echo $endTime;
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
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!