Date Format Conversion

Php newbie needs some help with date conversion please:
I have a perfect working sript that receives 2 different dates (data_start and data_end) in the form YYYY/MM/DD. The script sends these dates together with other info by e-mail to me, but I would like to read the dates as DD/MM/YYYY. For this purpose I have googled and found a script that is supposed to do the job.

With my limited knowlegde about PHP I have modified my script, but there is no change of the dates. I would very much appreciate if someone could tell me what has to be done, to convert these 2 dates. Many thanks. This is my modified script:

[php]<?php
session_start();

// ------------- CONFIGURABLE SECTION ------------------------

$mailto = ‘[email protected]’ ;
$subject = “Inquiry Form for TEST Villa R.S01” ;
$formurl = “http://www.phuket-beachvillas.com/villa-bb00/contact1/contact.php” ;
$errorurl = “http://www.phuket-beachvillas.com/errors.htm” ;
$thankyouurl = “http://www.phuket-beachvillas.com/thanks.htm” ;

// -------------------- END OF CONFIGURABLE SECTION ---------------

// load the variables form address bar

$Title = $_POST[‘Title’] ;
$name = $_POST[‘name’] ;
$email = $_POST[‘email’] ;
$phone = $_POST[‘phone’] ;
$fax = $_POST[‘fax’] ;
$nationality = $_POST[‘nationality’] ;
$data_start = $_POST[‘data_start’] ;
$data_end = $_POST[‘data_end’] ;
$result = $_POST[‘result’] ;
$pax = $_POST[‘pax’] ;
$payment = $_POST[‘payment’] ;
$children = $_POST[‘children’] ;
$roomtype = $_POST[‘roomtype’] ;
$appick = $_POST[‘appick’] ;
$purpose = $_POST[‘purpose’] ;
$comment= $_POST[‘comment’] ;
$verif_box = $_POST[“verif_box”];

// remove the backslashes that normally appears when entering " or ’
$Title = stripslashes($Title);
$name = stripslashes($name);
$email = stripslashes($email);
$phone = stripslashes($phone);
$fax = stripslashes($fax);
$nationality = stripslashes($nationality);
$data_start = stripslashes($data_start);
$data_end = stripslashes($data_end);
$result = stripslashes($result);
$pax = stripslashes($pax);
$children = stripslashes($children);
$payment = stripslashes($payment);
$appick = stripslashes($appick);
$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

// convert data_start to dd/mm/yyyy

// switch from mysql to UK format yyyy-mm-dd hh:ii:ss to dd/mm/yyyy hh:ii:ss (time is optional)
// allows use of non-numeric seperators so 2000X12X25 is a valid date
function data_start($timestamp, $date_seperator="/", $time_seperator=":", $seperator=" “)
{
ereg (”([0-9]{2,4})^0-9^0-9", $timestamp, $regs);
ereg ("([0-9]{2,4})^0-9^0-9^0-9^0-9^0-9", $timestamp, $regs);

if (sizeof($regs)>1)
{
	if (sizeof($regs)>4)
	{
		$data_start=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1].$seperator.$regs[4].$time_seperator.$regs[5].$time_seperator.$regs[6];
	}
	else
	{
		$data_start=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1];
	}
}
else
{
	$data_start="unavailable";
}
return $data_start;

}

// convert data_end to dd/mm/yyyy

// switch from mysql to UK format yyyy-mm-dd hh:ii:ss to dd/mm/yyyy hh:ii:ss (time is optional)
// allows use of non-numeric seperators so 2000X12X25 is a valid date
function data_end($timestamp, $date_seperator="/", $time_seperator=":", $seperator=" “)
{
ereg (”([0-9]{2,4})^0-9^0-9", $timestamp, $regs);
ereg ("([0-9]{2,4})^0-9^0-9^0-9^0-9^0-9", $timestamp, $regs);

if (sizeof($regs)>1)
{
	if (sizeof($regs)>4)
	{
		$data_end=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1].$seperator.$regs[4].$time_seperator.$regs[5].$time_seperator.$regs[6];
	}
	else
	{
		$data_end=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1];
	}
}
else
{
	$data_end="unavailable";
}
return $data_end;

}

// check to see if verificaton code was correct

if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){

// if verification code was correct send the message 

$http_referrer = getenv( “HTTP_REFERER” );
if (!isset($_POST[‘email’])) {
header(“Location: $formurl” );
exit ;
}

BEGIN OF CODE

all fields are empty : arrival, departure, pax and comments ==> error (works fine)

if(empty($data_start) && empty($data_end) && empty($pax)&& empty($comment)){
exit(“You have not specified any booking details”);
exit;
}

END OF CODE

if ( ereg( “[\r\n]”, $name ) || ereg( “[\r\n]”, $email ) ) {
header(“Location: $errorurl” );
exit ;
}

if (!isset($_POST[‘email’])) {
header(“Location: $formurl” );
exit ;
}

//===

//==
$messageproper =
“This message was sent from:\n” .
“$http_referrer\n\n” .
“------------------------- COMMENTS -------------------------\n\n” .
"Title: " .
“$Title\n” .
"Name: " .
“$name\n” .
"E-mail address: " .
“$email\n” .
"Phone no.: " .
“$phone\n” .
"Fax no.: " .
“$fax\n” .
"Nationality: " .
“$nationality\n” .
"Date of Arrival: " .
“$data_start\n” .
"Date of Departure: " .
“$data_end\n” .
"Length of stay: " .
“$result” .
“nights\n” .
"No. of Persons : " .
“$pax\n” .
"Including no. of children : " .
“$children\n” .
"Payment by : " .
“$payment\n” .
"Room Type : " .
“$roomtype\n” .
"Airport Pick-up : " .
“$appick\n” .
"Purpose : " .
“$purpose\n” .
"COMMENTS : " .
“$comment\n” .
“\n\n---------------- END OF MESSAGE -----------------\n” ;

mail($mailto, $subject, $messageproper, “From: “$name” <$email>\r\nReply-To: “$name” <$email>\r\nX-Mailer: chfeedback.php 2.04” );
header( “Location: $thankyouurl” );

// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');  
exit;


// if verification code was incorrect then return to contact page and show error
header("location:contact.php?Title=$Title&name=$name&email=$email&phone=$phone&fax=$fax&nationality=$nationality&data_start=$data_start&data_end=$data_end&result=$result&pax=$pax&children=$children&payment=$payment&appick=$appick&purpose=$purpose&comment=$comment&wrong_code=true");

exit;
} else {
echo “You have entered a wrong verification code. Please go back and try again.”;
exit ;
}

?>[/php]

This is the downloaded script, which should convert a date from YYYY/MM/DD to DD/MM/YYYY:

[php]// switch from mysql to UK format yyyy-mm-dd hh:ii:ss to dd/mm/yyyy hh:ii:ss (time is optional)
// allows use of non-numeric seperators so 2000X12X25 is a valid date
function uk_datetime_from_mysql($timestamp, $date_seperator="/", $time_seperator=":", $seperator=" “)
{
ereg (”([0-9]{2,4})^0-9^0-9", $timestamp, $regs);
ereg ("([0-9]{2,4})^0-9^0-9^0-9^0-9^0-9", $timestamp, $regs);

if (sizeof($regs)>1)
{
	if (sizeof($regs)>4)
	{
		$date=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1].$seperator.$regs[4].$time_seperator.$regs[5].$time_seperator.$regs[6];
	}
	else
	{
		$date=$regs[3].$date_seperator.$regs[2].$date_seperator.$regs[1];
	}
}
else
{
	$date="unavailable";
}
return $date;

}
[/php]

Try this:

[php]$date = “2011/09/30”; # YYY/MM/DD
$date = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$date); # DD/MM/YYYY[/php]

Let me know if it doesn’t work - not tested it yet.

Thanks for the advise.
It works indeed and converts the date given = 2011/09/30 (as a constant) into 30/09/2011.
However, my question is now: How do I replace that constant with the content of the fields “data_start” and “data_end”, which is each YYYY/MM/DD and put it into that same string ?
Again: My PHP knowledge is not sufficient to find a solution in this case. Do you have any idea ? Many thanks again.

In my example code, just changed the first line to:
[php]$date = $date_start;[/php]

OK - did that, but sorry, the date remains unchanged.
I don’t know how to read the second line exactly (can only interpret somehow), but are you sure that you replace the content of the field “data_start” at the end of the second line ? Sorry to bother you.

Sorry but I didn’t understand your last post apart from the date not changing.

Provided the string is definitely in the format “YYYY/MM/DD” and it is being passed to the preg_replace, echoing the variable $date should work. If you have rewritten my example code please post it.

I have 2 dates, which are selected from a calendar at my booking form at http://www.phuket-beachvillas.com/villa-rs01/contact/contact.php . The fields are
arrival date = “data_start” and
departure date = “data_end
These dates are definitely in the form YYYY/MM/DD, since otherwise the automatic night calculator would not output the number of nights between arrival and departure date. Here is the modified code:

[php]<?php
session_start();

// ------------- CONFIGURABLE SECTION ------------------------

$mailto = ‘[email protected]’ ;
$subject = “Inquiry Form for TEST Villa R.S01” ;
$formurl = “http://www.phuket-beachvillas.com/villa-bb00/contact1/contact.php” ;
$errorurl = “http://www.phuket-beachvillas.com/errors.htm” ;
$thankyouurl = “http://www.phuket-beachvillas.com/thanks.htm” ;

// -------------------- END OF CONFIGURABLE SECTION ---------------

// load the variables form address bar

$Title = $_POST[‘Title’] ;
$name = $_POST[‘name’] ;
$email = $_POST[‘email’] ;
$phone = $_POST[‘phone’] ;
$fax = $_POST[‘fax’] ;
$nationality = $_POST[‘nationality’] ;
$data_start = $_POST[‘data_start’] ;
$data_end = $_POST[‘data_end’] ;
$result = $_POST[‘result’] ;
$pax = $_POST[‘pax’] ;
$payment = $_POST[‘payment’] ;
$children = $_POST[‘children’] ;
$roomtype = $_POST[‘roomtype’] ;
$appick = $_POST[‘appick’] ;
$purpose = $_POST[‘purpose’] ;
$comment= $_POST[‘comment’] ;
$verif_box = $_POST[“verif_box”];

// remove the backslashes that normally appears when entering " or ’
$Title = stripslashes($Title);
$name = stripslashes($name);
$email = stripslashes($email);
$phone = stripslashes($phone);
$fax = stripslashes($fax);
$nationality = stripslashes($nationality);
$data_start = stripslashes($data_start);
$data_end = stripslashes($data_end);
$result = stripslashes($result);
$pax = stripslashes($pax);
$children = stripslashes($children);
$payment = stripslashes($payment);
$appick = stripslashes($appick);
$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

$date = $data_start; # YYY/MM/DD
$date = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$date); # DD/MM/YYYY

$date = $data_end; # YYY/MM/DD
$date = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$date); # DD/MM/YYYY

// check to see if verificaton code was correct

if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){

// if verification code was correct send the message 

$http_referrer = getenv( “HTTP_REFERER” );
if (!isset($_POST[‘email’])) {
header(“Location: $formurl” );
exit ;
}

BEGIN OF CODE

all fields are empty : arrival, departure, pax and comments ==> error (works fine)

if(empty($data_start) && empty($data_end) && empty($pax)&& empty($comment)){
exit(“You have not specified any booking details”);
exit;
}

END OF CODE

if ( ereg( “[\r\n]”, $name ) || ereg( “[\r\n]”, $email ) ) {
header(“Location: $errorurl” );
exit ;
}

if (!isset($_POST[‘email’])) {
header(“Location: $formurl” );
exit ;
}

//===

//==
$messageproper =
“This message was sent from:\n” .
“$http_referrer\n\n” .
“------------------------- COMMENTS -------------------------\n\n” .
"Title: " .
“$Title\n” .
"Name: " .
“$name\n” .
"E-mail address: " .
“$email\n” .
"Phone no.: " .
“$phone\n” .
"Fax no.: " .
“$fax\n” .
"Nationality: " .
“$nationality\n” .
"Date of Arrival: " .
“$data_start\n” .
"Date of Departure: " .
“$data_end\n” .
"Length of stay: " .
“$result” .
“nights\n” .
"No. of Persons : " .
“$pax\n” .
"Including no. of children : " .
“$children\n” .
"Payment by : " .
“$payment\n” .
"Room Type : " .
“$roomtype\n” .
"Airport Pick-up : " .
“$appick\n” .
"Purpose : " .
“$purpose\n” .
"COMMENTS : " .
“$comment\n” .
“\n\n---------------- END OF MESSAGE -----------------\n” ;

mail($mailto, $subject, $messageproper, “From: “$name” <$email>\r\nReply-To: “$name” <$email>\r\nX-Mailer: chfeedback.php 2.04” );
header( “Location: $thankyouurl” );

// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');  
exit;


// if verification code was incorrect then return to contact page and show error
header("location:contact.php?Title=$Title&name=$name&email=$email&phone=$phone&fax=$fax&nationality=$nationality&data_start=$data_start&data_end=$data_end&result=$result&pax=$pax&children=$children&payment=$payment&appick=$appick&purpose=$purpose&comment=$comment&wrong_code=true");

exit;
} else {
echo “You have entered a wrong verification code. Please go back and try again.”;
exit ;
}

?>[/php]

Okay after you’ve run the preg_replace, from there on you need to do what the replacement has been passed to ($data). In your code you are still using the original $data_start and $data_end. Change the preg_replaces to this:

[php]$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$data_end = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_end);[/php]

hmmm. I really don’t want to bother you too much, but this I had tried already earlier, unfortunately without the desired result. The dates are not converted. Maybe I leave it just the way it is and not wasting your costly time. Here is the part of the code that I have modified according to your latest advise above:

[php]$result = stripslashes($result);
$pax = stripslashes($pax);
$children = stripslashes($children);
$payment = stripslashes($payment);
$appick = stripslashes($appick);
$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

$date = $data_start; # YYYY/MM/DD
$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$date = $data_end; # YYYY/MM/DD
$data_end = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_end);

// check to see if verificaton code was correct

if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){

// if verification code was correct send the message 

$http_referrer = getenv( “HTTP_REFERER” );
if (!isset($_POST[‘email’])) {
header(“Location: $formurl” );
exit ;
}[/php]

Before the preg_replace custom set the data_start variable and see if it still doesn’t work - if this works, you may want to echo out the variable before the preg_replace to make sure there aren’t any spaces between slashes or anything like that.

[php]$data_start = ‘2011/10/01’;
$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);[/php]

There is no reason for this to not work provided part of the string contains a date with the format shown above. It will even work if it has a time afterwards (or before).

Thanks for all your help and sorry for the delay of my answer. I’m obviously at the other side of the pond with some 12hrs time difference.

OK - not sure if I understand correctly and so I’ve modified the code as below. Additionally I’ve made a screenshot of the input on my booking form. As you can see from the jpeg at http://www.phuket-beachvillas.com/villa-test/test_date.jpg, the arrival date (data_start) was given with 2011/10/18 and should be converted to 18/10/2011.

Instead the arrival date looks like this now: 2011/10/0111.
HAPPILY (!) I receive - a correctly converted departure date (data_end).
This is the e-mail returned from the code:

[b]This message was sent from:
http://www.phuket-beachvillas.com/villa-test/contact/contact.php

------------------------- COMMENTS -------------------------

Title: Mr
Name: frank
E-mail address: user1@user1
Phone no.:
Fax no.:
Nationality: Bermuda
Date of Arrival: 20/10/[color=red]0111[/color]
Date of Departure: 28/11/2011
Length of stay: 41nights
No. of Persons : 4
Including no. of children : 0
Payment by : MASTER
Room Type :
Airport Pick-up : yes - 4 Persons
Purpose : Holiday with friends
COMMENTS : test

---------------- END OF MESSAGE ----------------- [/b]

This is the last version of the modified code. (not sure if I did the right thing…)

[php]$pax = stripslashes($pax);
$children = stripslashes($children);
$payment = stripslashes($payment);
$appick = stripslashes($appick);
$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

$data_start = ‘2011/10/01’;
$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$date = $data_start; # YYYY/MM/DD
$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$date = $data_end; # YYYY/MM/DD
$data_end = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_end);

// check to see if verificaton code was correct

if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){

// if verification code was correct send the message 

[/php]

No I meant to just do this:

[php]$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

$data_start = ‘2011/10/01’;
$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$date = $data_end; # YYYY/MM/DD
$data_end = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_end);

// check to see if verificaton code was correct
if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){[/php]

This was to check that the variable was being correctly altered. It appears to be working correctly though, even though the method has been applied twice to that string. Return the code to the following, if it doesn’t work at all with this then something really strange is going on:

[php]$purpose = stripslashes($purpose);
$comment= stripslashes($comment);

$data_start = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_start);

$data_end = preg_replace(’/([0-9]{2,4})/([0-9]{1,2})/([0-9]{1,2})/’,’$3/$2/$1’,$data_end);

// check to see if verificaton code was correct
if(md5($verif_box).‘a4xn’ == $_COOKIE[‘tntcon’]){[/php]

Smokey PHP
:o
YOU’RE THE BEST !
THANKS a thousand times for your professional advise and for your extreme patience with stupid me !
I now receive both dates correctly:
Date of Arrival: 18/10/2011
Date of Departure: 28/11/2011

How can I tank you other than saying THANKS ? If you were female I would send you this: :-*
I’m now going to implement this additional code into some 84 different forms in my webs.
Meanwhile have a nice Sunday, or whatever is left of it at your home.

You made someone really happy today !
All the Best to you.
Frank

You’re more than welcome. I’m glad to have helped. Good luck with the rest of your project.

Sponsor our Newsletter | Privacy Policy | Terms of Service