drwo2014 2014-07-10 01:53
浏览 34
已采纳

从日期范围确定Midnights的数量

I have a reservation system, which charges a daily rate for overnights. I'm trying to figure out how to calculate the number of times midnight has been crossed for a given date range in PHP.

I have 2 unix timestamps:

$start_date        = 1404349200; // 07/02/14 07:00:00 pm
$end_date          = 1404936000; // 07/09/14 02:00:00 pm
$total_time_here   = $end_date - $start_date; // number of seconds here
$number_overnights = (int) ($total_time_here / 60 / 60 / 24); // calculate number of 24-hour periods

$number_overnights evaluates to 6, however the number of times midnight occurred for that date range is 7, which is what I want to count. This happens when the $start_date happens later in the day than the $end_date (7:00pm vs 2:00pm).

Both the $start_date and $end_date are in the same time zone, and are stored in UTC in the database and then converted back in PHP.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • doushishi2415 2014-07-10 01:59
    关注

    I think this might work:

    $start_date = strtotime(date('Y-m-d 00:00:00', 1404349200));
    $end_date = strtotime(date('Y-m-d 23:59:59', 1404936000));
    $total_time_here   = $end_date - $start_date;
    $number_overnights = (int) ($total_time_here / 60 / 60 / 24);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM