PHP - MYSQL - Dates
Discussion
Aargh, I've wasted enough time on this. I want a simple form field where I can input dates in a UK format and then swap them to MYSQL's native YYYYMMDD format in the background and sling them in my database.
Has anyone got an example of how to do it? I can pull dates out and format them to my hearts content using date('d-m-Y') but swapping 06/02/2008 to 2008-02-06 ready to input is being a right pain.
Heeelp, I'm going mad
Has anyone got an example of how to do it? I can pull dates out and format them to my hearts content using date('d-m-Y') but swapping 06/02/2008 to 2008-02-06 ready to input is being a right pain.
Heeelp, I'm going mad
Sorry but I stopped reading after YYYYMMDD - I ing hate doing date conversions. Piss boiling to the extreme.
However, best bet is to get it into the Integer timsestamp format - you know that ugly long number that started at zero in 1972 and then use the date formater to get it into the one you need.
Clicky
A bit of javascript client side will format for you ready for the HTTP Post, then just format it back again before putting into the DB.
Although, with one of my systems I just post the date straight into PHP and then use that directly in SQL, so maybe you need to get your MySQL instance running on the correct date format and go from there.
I think MySQL picks it up from the OS - I know once I told my Linux platform to use UTC everything was a breeze.
Good Luck! (You'll likely need it!)
if(ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})",$_POST['date_from_form'],$date)){
$mysql_date = $date[3].'-'.$date[2].'-'.$date[1];
}
echo $mysql_date;
edit: this is better:
if(ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})",$_POST['date_from_form'],$date)){
$mysql_date = date('Y-m-d',mktime(0,0,0,$date[2],$date[1],$date[3]));
}
echo $mysql_date;
Works for short-dates too, so 1/2/90 will be 1st feb 1990, and so will 01/02/1990.
$mysql_date = $date[3].'-'.$date[2].'-'.$date[1];
}
echo $mysql_date;
edit: this is better:
if(ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})",$_POST['date_from_form'],$date)){
$mysql_date = date('Y-m-d',mktime(0,0,0,$date[2],$date[1],$date[3]));
}
echo $mysql_date;
Works for short-dates too, so 1/2/90 will be 1st feb 1990, and so will 01/02/1990.
Edited by Altrezia on Wednesday 6th February 15:01
The initial code didn't work, however it was an invaluable help as it led me to further investigate the ereg expression and come up with this which also includes some date validation!:
echo "<br>Sample output from the input box <br>";
$date2 = $_POST['Datebox'];
if (ereg ("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})",$_POST['Datebox'], $regs)) {
echo "$regs[3]/$regs[2]/$regs[1]" . "<br>";
If(!checkdate($regs[2], $regs[1], $regs[3])){
echo "There appears to be a problem with the validity of the date entered";
}else {
echo "The entry date appears to be correct ";
}
} else {
echo "Invalid date format: " . $_POST['Datebox'] ."<br>";
echo "Please enter the date in the format dd/mm/yyyy";
}
I *think* on my OS I needed the additional $regs variable, either way, it only blummin works!
I'm not sure I've got the heart to examine mktime after all that
And Ex, I know exactly what you mean, last time I got round it with a .js calendar that did the inputting for me without having to swap variables around
echo "<br>Sample output from the input box <br>";
$date2 = $_POST['Datebox'];
if (ereg ("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})",$_POST['Datebox'], $regs)) {
echo "$regs[3]/$regs[2]/$regs[1]" . "<br>";
If(!checkdate($regs[2], $regs[1], $regs[3])){
echo "There appears to be a problem with the validity of the date entered";
}else {
echo "The entry date appears to be correct ";
}
} else {
echo "Invalid date format: " . $_POST['Datebox'] ."<br>";
echo "Please enter the date in the format dd/mm/yyyy";
}
I *think* on my OS I needed the additional $regs variable, either way, it only blummin works!
I'm not sure I've got the heart to examine mktime after all that
And Ex, I know exactly what you mean, last time I got round it with a .js calendar that did the inputting for me without having to swap variables around
TheGriffalo said:
If the MySQL bods coulr write a few lines of code to interpret date formats they'd save the whole world a heap of time
What you've got to remember Rob, is that getting it out is a whole lote easier than putting it in! Oh Try PostGreSQL - apparently it's much better, but I've still to convince myself - in fact get it working - still happy to be convinced - it seems there's many on here who rate it
The Excession said:
TheGriffalo said:
If the MySQL bods coulr write a few lines of code to interpret date formats they'd save the whole world a heap of time
What you've got to remember Rob, is that getting it out is a whole lote easier than putting it in! Oh Try PostGreSQL - apparently it's much better, but I've still to convince myself - in fact get it working - still happy to be convinced - it seems there's many on here who rate it
TheGriffalo said:
and the sooner I stop writing code the better
Be careful what you wish for.... one minute you are writing date conversion code the next you have a Customer asking you to elaborate more into your 175 page contract proposal....Still, I've got great people that I'm working with and they understand exactly how to get things done. I know you're suffereing a bit at the moment and you really shouldn't be writing any code.
Give me a bell when you have a minute.
best
Ade
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff