Travel2Learn
Travel2Learn

Reputation: 19

Date formating not working

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

Upvotes: 0

Views: 49

Answers (1)

user5125586
user5125586

Reputation:

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');

Upvotes: 2

Related Questions