weixin_33701251 2018-08-22 00:27 采纳率: 0%
浏览 40

日期格式不起作用

I have been working with PHP for several months and just started teaching myself ajax. My first project is to send variables from a PHP form via jQuery to another PHP which serves as the action. I want to find the difference between two dates&times and return an echo response of numerous calculations back to the PHP form page. The good news is that ajax works as expected and displays the calculations on the form page, but only if I use a hardcode date-time ($start) to begin the calculations. If I try to format a date ($start) with the variables from the form page, ajax returns nothing. If this question has been answered, please direct me to that location.

$TravelYear  = $_POST['TravelYear'];  
$TravelMonth = $_POST['TravelMonth'];  
$TravelDate  = $_POST['TravelDate'];  
$TravelHour  = $_POST['TravelHour'];  
$TimeZone   = $_POST['TimeZone'];  
date_default_timezone_set($TimeZone);  

// this does not work  
$start = date('Y-m-d G:i:s',strtotime($TravelYear.'-'.$TravelMonth.'-'.$TravelDate.' '.$TravelHour.":00:00")); 

// this works  
$start = date_create('2015-07-10 14:30:00');

$end = date_create(); // Current time and date  
$diff = date_diff( $start, $end ); // Find the difference

// lots of calculations
  • 写回答

1条回答 默认 最新

  • 零零乙 2018-08-22 00:51
    关注

    The problem is that date_diff expects a datetime object not a date string.

    According to the documentation, date_diff is an alias for DateTime::diff()

    Use:

    $start = date_create($TravelYear.'-'.$TravelMonth.'-'.$TravelDate.' '.$TravelHour.':00:00');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败