Skip to content

Instantly share code, notes, and snippets.

@JingwenTian
Created September 29, 2014 04:09
Show Gist options
  • Save JingwenTian/58ae4c6fc9038f43202d to your computer and use it in GitHub Desktop.
Save JingwenTian/58ae4c6fc9038f43202d to your computer and use it in GitHub Desktop.
PHP中有关日期计算的一些片段
<?php
/**
* 根据活动开始与结束的时间戳罗列出中间所有日期
* @param $start 开始时间戳
* @param $end 结束时间戳
* @return $dates 日期数组
*/
function getDateArr ( $start, $end )
{
$date = array();
$s = abs((strtotime(date('Y-m-d',$start))-strtotime("xxxx-xx-xx"))/86400);
$e = abs((strtotime(date('Y-m-d',$end))-strtotime("xxxx-xx-xx"))/86400);
$num = $e - $s;//活动总天数
for($i = 0;$i<=$num;$i++){
$date[] = date('Y-m-d',$start+86400*$i);
}
return $date;
}
/*应用
$start = 1405440000;//开始时间
$end = 1419955200;//结束时间
$date = getDateArr ( $start, $end );
foreach ($date as $key => $value) {
$sql="select * from TABLE where DATE_FORMAT(FROM_UNIXTIME(time),'%Y-%m-%d') = {$date[$key]}";
....
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment