douxun2018 2010-07-30 12:30
浏览 22
已采纳

在PHP中,如果变量包含日期时间,那么运算符会做什么?

My background is C, Java, C#, and VB.NET, but I have to work on some basic PHP stuff. I've gotten to this line of code:

if($flag) $event_end--;

What exactly is being decremented? The raw ticks? $flag (I think) represents if the event is an all-day event, where the start would be 7/1/2010 and end would be 7/2/2010. Does the -- essentially make $event_end [7/1/2010 11:59:59.99999 PM]?

-- edit: ok, cardinal rule of technical questions: explain what you're talking about. guilty. $event_end is orginally being set using the mktime() function. does that help?

  • 写回答

4条回答 默认 最新

  • duanchao5258 2010-07-30 12:39
    关注

    Has a function such as strtotime() been called on the datetime first? This function will return the Unix Timestamp corresponding to the date and time. If the time is 0 (i.e. midnight), subtracting 1 from it (i.e. taking away 1 second), the result will be a time of 23:59:59.

    $event_end = '2010-07-30 00:00:00';
    $event_end = strtotime($event_end);
    $event_end--;
    $event_end = date("Y-m-d H:i:s",$event_end);
    echo $event_end;  //This will result in '2010-07-29 23:59:59'
    

    PHP sees datetime values (as queried from MySQL for example) as simple strings unless they are DateTime objects in which case I don't think subtracting from it would do anything. The only way that operator would affect the value is if it was a timestamp integer.

    EDIT: Ah, I see it uses mktime(). This also returns a Unix Timestamp and therefore can be operated on mathematically. So, to answer your question, indeed the $event_end--; is subtracting one second.

    Updated Code:

    $event_end = mktime(0, 0, 0, 7, 30, 2010);
    $event_end--;
    $event_end = date("Y-m-d H:i:s",$event_end);
    echo $event_end;  //This will result in '2010-07-29 23:59:59'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题