Hey guys,
Here's a couple of functions I wrote that I use in my core functions include file at the top of my pages... these are not exact (as they are for US date formatting) but could easily be modified for use in any format.
The first one is what I call goodDate - it currently takes database formatted date and returns the US date formatting of "M/D/YY".
// goodDATE FUNCTION
function goodDate($datetochange) {
$date_var = strtotime($datetochange);
echo date('n/j/y', $date_var);
}
If you need to modify another variable with this, just chance the echo command to "return".
The second function was written so when a user is updating dates in a form, their submission will be changed from "M/D/YY" to the database format. By enabling a function to change the date, you don't have to rely on client or JS validation to actually handle the reformat allowing for easier validation of the input.
// DATABASE DATE CONVERSION (ASSUMES DATE FORMAT IS MM/DD/YY)
function dataDate($datetoconvert) {
$dateDataDate = explode('/', $datetoconvert);
if ($dateDataDate[2] >= 40 && $dateDataDate[2] <= 99) {
$dateDataYear = '19'.$dateDataDate[2];
} else {
$dateDataYear = '20'.$dateDataDate[2];
}
if (strlen($dateDataDate[0]) == 1) {
$dateDataMonth = '0'.$dateDataDate[0];
} else {
$dateDataMonth = $dateDataDate[0];
}
if (strlen($dateDataDate[1]) == 1) {
$dateDataDay = '0'.$dateDataDate[1];
} else {
$dateDataDay = $dateDataDate[1];
}
$dataDateFinal = $dateDataYear.'-'.$dateDataMonth.'-'.$dateDataDay;
return $dataDateFinal;
}
To use this... just take your post variable as it gets entered into the database and wrap it with dataDate(variable).
Again, this was set up to handle US dates but as you can see, could be easily modified for any format.
Cheers,
Brian