Click to See Complete Forum and Search --> : [RESOLVED] Convert Date
Pino
Oct 10th, 2005, 08:26 AM
I ams torign a date in a mysql db it is inputted as DD/MM/YYYY and i need to transform it to YYYY/MM/DD then also swith it back to DD/MM/YYYY
Anyone got a fucntion that could help me ?
visualAd
Oct 10th, 2005, 10:13 AM
preg_match() (http://www.php.net/preg_match)
Here is a function which I use to convert a date in the format of yyyy-mm-dd to a UNIX timestamp. Obviously, you'll need to modify this to suit your needs; it will be benificial to you if you look at the Pattern Syntax (http://www.php.net/manual/en/reference.pcre.pattern.syntax.php) for PCRE too.
/**
* Converts a date from FoxPro format to a UNIX time stamp.
*
* Converts a date in the format yyyy-mm-dd to a UNIX time stamp which can be used
* with PHP's date/time functions.
*
* @author Adam Delves (adam @ sccode . com)
* @copyright Copyright © all rights reserved Adam Delves
*
* @param string $date A date in the format yyyy-mm-dd
* @return mixed null for a null date or a timestamp for a valid date as returned by mktime()
*/
function format_date($date)
{
/* REGEXP: matches a stirng in the format dddd-dd-dd where
d is any decimal number 0-9
*/
$regex = "/^(\d{4})\-(\d{2})\-(\d{2})$/";
if ($date == '1899-12-30' || $date == '' ) { // empty/null date
return null;
} else if (preg_match($regex, $date, $matches)) {
return mktime(0,0,0, $matches[2], $matches[3], $matches[1]);
} else {
throw new Exception('Invalid Date Format'); // PHP 5 only
}
}
Pino
Oct 10th, 2005, 11:46 AM
Thanks for the help once again adam.
I have done this using the SPLIT() function and its workign great now :)
Thnaks
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.