donglie7778 2016-03-28 16:24
浏览 21
已采纳

如何在PHP中添加格式化日期

this is what I have got:

$_SESSION["chosen_exam_start_time"] = new DateTime();
$schedule_date =  $_SESSION["chosen_exam_start_time"]; 
$schedule_date->setTimeZone(new DateTimeZone('Europe/London'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

so I have a date passed within the session called "chosen_exam_start_time".

Now what I am struggling to do is add on $triggerOn.

So currently if I do

die(var_dump($triggerOn));

I get :

string(19) "2016-03-28 17:17:57"

What I want to do is if I have another variable which tells me a value like 10, how would I be able to add that value on to the $triggerOn. So that it will be:

string(19) "2016-03-28 17:27:57"

What I have done up to now is :

            $_SESSION["chosen_exam_start_time"] = new DateTime();
            $schedule_date =  $_SESSION["chosen_exam_start_time"]; 
            $schedule_date->setTimeZone(new DateTimeZone('Europe/London'));
            $triggerOn =  $schedule_date->format('Y-m-d H:i:s');
            $value = 10;

            $triggerOn->modify((int) $value.' minute');


            die(var_dump($triggerOn));

However this die is not printing anything out now

  • 写回答

2条回答 默认 最新

  • dongtangze6393 2016-03-29 07:56
    关注

    I doesn't make sense to make any sort of calculation using strings. Just imagine you had this:

    $price = 'Thirty five point nine';
    $discount = 'Twenty percent';
    

    ... and you wanted to apply the discount ;-)

    If your current architecture makes it impossible to keep the original DateTime object (I don't know, I don't have enough information for that) you'd better create a new one:

    <?php
    
    $triggerOn = '2016-03-28 17:17:57';
    $minutes_to_add = 10;
    
    $triggerOnDateTime = new DateTime($triggerOn, new DateTimeZone('Europe/London'));
    $triggerOnDateTime->modify( sprintf('%d minutes', $minutes_to_add) );
    $triggerOn = $triggerOnDateTime->format('Y-m-d H:i:s');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?