Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello forum members,

 

I'm using the datetime.php script from chapter 16, but am having some difficulties in changing the format of the dates. The format of the dates is 'm/d/Y;  which I've changed to 'd/m/Y'. But how do I convert a date such as 17/04/2014 to 17 April 2014 or to Thursday, 17 April 2014? I've tried various date functions such as strtotime - however this only accepts "American month, day and year"?

 

Please help me with this if you can.

 

 

Thank you in advance.

 

Link to comment
Share on other sites

Thank you for the link. I changed the script a bit and now I receive the error message:

 

 

One or both of the submitted dates was invalid

 

The datepicker uses the dd/mm/yy format, such as 22/04/2014. Must I change $format = 'm/d/Y' to $format = 'd/m/Y'? Also, I'm not sure if the day and month variables must be swapped around in the below code:

if (isset($_POST['start'], $_POST['end'])) {

	// Call the validation function on both dates:
	if ( (list($sm, $sd, $sy) = validate_date($_POST['start'])) && (list($em, $ed, $ey) = validate_date($_POST['end'])) ) {
		
		// If it's okay, adjust the DateTime objects:
		$start->setDate($sy, $sm, $sd);
		$end->setDate($ey, $em, $ed);

Here is the full original script:

<?php # Script 16.5 - datetime.php

// Set the start and end date as today and tomorrow by default:
$start = new DateTime();
$end = new DateTime();
$end->modify('+1 day');

// Default format for displaying dates:
$format = 'm/d/Y';

// This function validates a provided date string.
// The function returns an array--month, day, year--if valid.
function validate_date($date) {
	
	// Break up the string into its parts:
	$array = explode('/', $date);
	
	// Return FALSE if there aren't 3 items:
	if (count($array) != 3) return false;
	
	// Return FALSE if it's not a valid date:
	if (!checkdate($array[0], $array[1], $array[2])) return false;

	// Return the array:
	return $array;
	
} // End of validate_date() function.

// Check for a form submission:
if (isset($_POST['start'], $_POST['end'])) {

	// Call the validation function on both dates:
	if ( (list($sm, $sd, $sy) = validate_date($_POST['start'])) && (list($em, $ed, $ey) = validate_date($_POST['end'])) ) {
		
		// If it's okay, adjust the DateTime objects:
		$start->setDate($sy, $sm, $sd);
		$end->setDate($ey, $em, $ed);
		
		// The start date must come first:
		if ($start < $end) {
			
			// Determine the interval:
			$interval = $start->diff($end);
			
			// Print the results:
			echo "<p>The event has been planned starting on {$start->format($format)} and ending on {$end->format($format)}, which is a period of $interval->days day(s).</p>"; 
			
		} else { // End date must be later!
			echo '<p class="error">The starting date must precede the ending date.</p>';
		}
			
	} else { // An invalid date!
		echo '<p class="error">One or both of the submitted dates was invalid.</p>';
	}
		
} // End of form submission.

// Show the form:
?>

Thank you!

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

The validate_date() method returns the month, day, and year in the same order as they are coming in in the string. So if you're passing DD/MM/YYYY, you'll get back day, month, year, not month, day, year, as your code presumes.

Link to comment
Share on other sites

Hi Larry,

 

thank you for replying to my thread. I've set the datepicker's date format to dd/mm/yy and also changed $format = 'm/d/Y' to $format = 'd/m/Y'. With this I get the error message "One or both of the submitted dates was invalid.". I think the checkdate function could be the problem because the checkdate format is month, day, year and what I'm putting in is day, month, year?

 

Thank you. Happy Easter by the way.

Link to comment
Share on other sites

Happy Easter to you as well! Yes, because of the format you're using for your dates, the checkdate() function will also fail. Keep in mind that validate_date() is just splitting on the slashes, so you'll need to update all the code to adjust for the input format.

Link to comment
Share on other sites

Hi Larry,

 

Happy Easter to you as well!

 

 

Thank you.

 

 

I have changed the validate_date function by removing the checkdate validation. My incoming string is now in the format 22-04-2014 and I'm using strtotime to validate the date. This works but if someone enters a date such as  2014-05-09 I receive an error stating “DateTime::setDate() expects parameter 1 to be long, string given”.  I don't know how I can force strtotime to only accept the dd-mm-yy format. Do you have any other suggestions? I'd really like to have a day month year format but if this doesn't work then I'll probably have to use the format in the original script.

 

 

My validate_date function now solely consists of the following:

function validate_date($date) {
	
	if(strtotime($date)) {

	return $date;

	}
} // End of validate_date() function.

Thank you, and I appreciate your help with this!

Link to comment
Share on other sites

Write a function where you create a DateTime object inside a try-catch, return true inside the try and return false if an exception is thrown. That is what I use to do.

 

function isValidDate( $date )

{

try {

$dateTime = new DateTime($date);

return true;

}

catch ( Exception $e )

{

return false;

}

}

 

if ( isValidDate($someDate) )

{

// valid

}

Link to comment
Share on other sites

  • 2 weeks later...

Hello Antonio,

 

my apologies for the late reply! Due to unforseen circumstances I haven't had a chance to test the try-catch.  I'll let you know how it goes.

 

 

I'm currently occupied with studies and begin the job in June. :)

 

 

 

Okay, that's great! Have a good day.

Link to comment
Share on other sites

 Share

×
×
  • Create New...