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]