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

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突