PHP时间
本文环境 PHP7.3 不懂的可以评论或联系我邮箱:owen@owenzhang.com 著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。
获取2个时间内的所有日期
/**
* 获取2个时间内的所有日期
* @access public
* @param string $startDate 开始时间2019-12-30
* @param string $backDate 结束时间2020-01-02
* @return array 返回类型
* array(4) {
* [0]=>
* string(10) "2019-12-30"
* [1]=>
* string(10) "2019-12-31"
* [2]=>
* string(10) "2020-01-01"
* [3]=>
* string(10) "2020-01-02"
* }
*/
function getBetweenTimeArr(string $startDate, string $backDate): array
{
if (!$startDate || !$backDate) {
return [];
}
$dtStart = strtotime($startDate);
$dtEnd = strtotime($backDate);
$getBetweenTimeArr = [];
while ($dtStart <= $dtEnd) {
$getBetweenTimeArr[] = date('Y-m-d', $dtStart);
$dtStart = strtotime('+1 day', $dtStart);
}
return $getBetweenTimeArr;
}
日期转时间戳
strtotime
PHP获取上周、本周、上月、本月、本季度、上季度时间方法
这篇文章介绍的内容是关于PHP获取上周、本周、上月、本月、本季度、上季度时间方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
echo "今天:".date("Y-m-d")."
";
echo "昨天:".date('Y-m-d', strtotime('-1 day')), "
";
echo "明天:".date("Y-m-d",strtotime("+1 day")). "
";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "
";
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "
";
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "
";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."
";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
";
开发过程中,常用日期处理函数,时间戳处理函数等,如下讲解:
echo date('Y-m-d h:i:s', time());
//运行结果(年月日时分秒,“-”间隔):2014-09-12 06:28:32
echo date('Y-m-d', time());
//运行结果(年月日,“-”间隔):2014-09-12
echo date('Y-m-d', strtotime(date('Y-m-d', time() - 86400)));
//运行结果(当前日期前一天的年月日,“-”间隔):2014-09-11
echo date('Ymd', time());
//运行结果(年月日,无间隔):20140912
echo date('m-d', time());
//运行结果(月日,“-”间隔):09-12
echo str_replace("-", "月", date('m-d', time() - date('w', time()) * 86400)) . "日";
//运行结果(月日,汉字显示间隔):09月12日
echo date('w', time());
//运行结果(星期几):5
echo time();
//运行结果(当前日期时间的秒数):1410503809
echo strtotime(date('Y-m-d', time()));
//运行结果(当前日期秒数,具体到天):1410503809
echo date('Y-m-d', strtotime(date('Y-m-d', time())) - date('w', strtotime(date('Y-m-d', time()))) * 86400);
//运行结果(当前日期所属自然周的起始日期即周日的日期,具体到天,“-”间隔):2014-09-07
//php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//php获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
/**
* date 2015-04-11
* author http://www.lai18.com
**/
echo date("Ymd", strtotime("now")), "\n";
echo date("Ymd", strtotime("-1 week Monday")), "\n";
echo date("Ymd", strtotime("-1 week Sunday")), "\n";
echo date("Ymd", strtotime("+0 week Monday")), "\n";
echo date("Ymd", strtotime("+0 week Sunday")), "\n";
echo "*********第几个月:";
echo date('n');
echo "*********本周周几:";
echo date("w");
echo "*********本月天数:";
echo date("t");
echo "*********";
echo '上周起始时间:';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"))), "\n";
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"))), "\n";
echo '本周起始时间:';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))), "\n";
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))), "\n";
echo '上月起始时间:';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y"))), "\n";
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))), "\n";
echo '本月起始时间:';
echo date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y"))), "\n";
echo date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y"))), "\n";
$season = ceil((date('n')) / 3);//当月是第几季度
echo '本季度起始时间:';
echo date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))), "\n";
echo date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))), "\n";
$season = ceil((date('n')) / 3) - 1;//上季度是第几季度
echo '上季度起始时间:';
echo date('Y-m-d H:i:s', mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'))), "\n";
echo date('Y-m-d H:i:s', mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'))), "\n";
Buy me a cup of coffee 🙂
觉得对你有帮助,就给我打赏吧,谢谢!