PHP - MYSQL - Dates

Author
Discussion

TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
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 banghead


TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
That will format the output as expected... But only if the data is entered in the MySQL format in $_POST['your_form_date'] banghead

TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
Ramped up the debugging output...

Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970

irked

TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
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! bouncethumbup

I'm not sure I've got the heart to examine mktime after all that hehe



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 smash

TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
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 yes

TheGriffalo

Original Poster:

72,857 posts

241 months

Wednesday 6th February 2008
quotequote all
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 yes
What you've got to remember Rob, is that getting it out is a whole lote easier than putting it in! Oh yes

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
Learn another syntax.... You're having a laugh! biggrin I can just about do what I need with MySQL and the sooner I stop writing code the better wink