dongpu1879 2014-07-28 09:42
浏览 48
已采纳

日期转换奇怪的输出在PHP中

I have problem to convert one of my dates in YYY-d-m format.

I have the array $searchQuery, which when print using print_r, prints the foloowing output:

Array ( [selectedArea] => 0 
        [checkin] => 30/07/2014 
        [checkout] => 01/08/2014 
        [rooms] => 1 
        [adults] => 2 
        [childrens] => 0 ) 

I have the following code:

        echo "Checkin is:" .$searchQuery['checkin']."<br />";
        $what = strtotime($searchQuery['checkin']);
        echo "New checkin is:" .$newCheckin = date('Y-m-d', $what) ."<br />"; 
        echo "Checkout is:" .$searchQuery['checkout']."<br />";
        $newCheckOutTemp = strtotime($searchQuery['checkout']);
        echo "New checkout is:" .$newCheckout = date('Y-m-d', $newCheckOutTemp) ."<br/>"; 

Which prints the following:

Checkin is:30/07/2014
New checkin is:1970-01-01 ------>????    
Checkout is:01/08/2014
New checkout is:2014-01-08

I have two questions...why the first date is printing 1970-01-01 when converted, and second, how can i the difference in days between those 2 dates.

Any help will be deeply appreciated.

Regards, John

  • 写回答

3条回答 默认 最新

  • douxing6434 2014-07-28 09:48
    关注

    try with datetime()

    $now = new DateTime(str_replace('/', '-','30/07/2014'));
    echo $now->format('Y-m-d');
    $newCheckOutTemp = new DateTime(str_replace('/', '-','01/08/2014'));
    echo $newCheckOutTemp->format('Y-m-d'); 
    

    or with date()

    echo date('Y-m-d', strtotime(str_replace('/', '-',$what)));
    echo $newCheckout = date('Y-m-d', strtotime(str_replace('/', '-',$newCheckOutTemp)));
    

    For day difference here so many answers :- Date Difference in php on days?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)