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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题