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条)

报告相同问题?

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错