一个 php 获得 某天开始/结束 某周开始结束 某月开始结束代码

2016-02-26 12:13:09 +08:00
 xiongjiabin

//do some test 求今天的开始时间和结束时间
$timestamp = time();
$beginOfDay = strtotime("midnight", $timestamp);
$endOfDay = $beginOfDay + 60 * 60 * 24 - 1;

echo date('Y-m-d H:i:s e',$beginOfDay);
echo '<br>';
echo date('Y-m-d H:i:s e',$endOfDay);

//这个周末的开始时间和结束时间
$beginOfWeek = strtotime('monday this week');
$endofWeek = $beginOfWeek + 7 * 60 * 60 * 24 -1;
echo '<br>';
echo date('Y-m-d H:i:s e',$beginOfWeek);
echo '<br>';
echo date('Y-m-d H:i:s e',$endofWeek);

$beginOfWeek = strtotime('monday this week') - 60 * 60 * 24 * 7;
$endofWeek = $beginOfWeek + 7 * 60 * 60 * 24 -1;
echo '<br>';
echo date('Y-m-d H:i:s e',$beginOfWeek);
echo '<br>';
echo date('Y-m-d H:i:s e',$endofWeek);

//时,分,秒,月,日,年
$month = 2;
$year = 2016;
$beginOfMonth = mktime(0,0,0,$month,1,$year) ;
$endofMonth = mktime(0,0,0,$month + 1,1,$year) - 1;;
echo '<br>';
echo date('Y-m-d H:i:s e',$beginOfMonth);
echo '<br>';
echo date('Y-m-d H:i:s e',$endofMonth);

1941 次点击
所在节点    分享创造
2 条回复
wd0g
2016-02-29 10:02:05 +08:00
代码可以在简化下撒
inmyfree
2016-03-02 10:05:41 +08:00
第一个...

$timestamp = time();
echo $startDate = date("Y-m-d 00:00:00 e",$timestamp);
echo $endDate = date("Y-m-d 23:59:59 e",$timestamp );


第二个

$timemsp = time();
$weekIndex = date ( "w", $timemsp ); // 一周的第几天
$endIndex = 7 - $weekIndex; // 周日是几天后
$weekIndex = $weekIndex - 1;
echo $weekStart = date("Y-m-d 00:00:00 e", strtotime ("-$weekIndex day", $timemsp )); // 计算周一
echo $weekEnd = date("Y-m-d 23:59:59 e", strtotime ("+$endIndex day", $timemsp )); // 计算周末


第三个

$timemsp = time();
echo $firstday = date('Y-m-01 00:00:00 e',$timemsp);//开始时间所在月的第一天
echo $lastday = date('Y-m-d 23:59:59 e',strtotime("$firstday +1 month -1 day",$timemsp));//这个月最后一天

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/259258

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX