Page 1 of 1
Date Formats
Posted: Tue Apr 17, 2007 7:59 pm
by pcatwood1
I know this is simple, but I am new to PHP and need help. I am using PHP with PHPMyAdmin and MySQL.
I am wanting to capture the user's date of birth. I know the database stores the timestamp and date fields as yyyy-mm-dd. As long as the user enters their date of birth as yyyy-mm-dd, it stores in the database fine. My users continue to enter it as mm/dd/yyyy out of habit and that does not store.
Any ideas on how to let them enter as mm/dd/yyyy and then store it to the database and then for the user to confirm, pull it back out of the database in mm/dd/yyyy format?
Posted: Tue Apr 17, 2007 8:19 pm
by volka
strtotime is your friend
Code: Select all
$userinput = '04/18/2007';
$ts = strtotime($userinput);
echo date('Y-m-d', $ts);
Posted: Tue Apr 17, 2007 8:21 pm
by John Cartwright
Have you considered using a javascript date picker?
Burrito has recently developed a very promising one found
here
Re: Date Formats
Posted: Wed Apr 18, 2007 10:56 am
by RobertGonzalez
pcatwood1 wrote:I know this is simple, but I am new to PHP and need help. I am using PHP with PHPMyAdmin and MySQL.
I am wanting to capture the user's date of birth. I know the database stores the timestamp and date fields as yyyy-mm-dd. As long as the user enters their date of birth as yyyy-mm-dd, it stores in the database fine. My users continue to enter it as mm/dd/yyyy out of habit and that does not store.
Any ideas on how to let them enter as mm/dd/yyyy and then store it to the database and then for the user to confirm, pull it back out of the database in mm/dd/yyyy format?
You can make your format however you want in the database I believe. But since you should be validating times (along with all other user input) anyway, it is a good idea to use what volka suggested.
Posted: Wed Apr 18, 2007 11:04 am
by bert4
Why don't you force them to do it right?
Have three drop down boxes; one with the days, one with the months, (jan feb (no numbers) etc) and one with the years ?
You can later assemble the date as you want....
Posted: Wed Apr 18, 2007 1:00 pm
by onion2k
volka wrote:strtotime is your friend
Correction: strtotime is your friend if you only get visitors from one area of the world. If you're going to get UK and US visitors then strtotime will not work. If Feyd enters 10/03/2007 into a website he probably means October 3rd 2007 ... If I enter 10/03/2007 I mean March 10th 2007. Using a date picker is the only way to be certain.
dateformat
Posted: Wed Apr 18, 2007 7:10 pm
by pcatwood1
I just don't understand why this is so hard to do. My timestamp that is stored in the database is 2007-04-17. I want it to display as 04-17-07. I am trying the date function, but it is displaying as 12-31-1969. I am assuming that this is the unix timestamp, but what can I do to just transform the date I have stored. Nothing seems to be working.
For user input, if I have to store the date of birth in 3 different drop downs, I might have to do that, but would rather just let them enter the date as 3-21-1997.
FYI - I am in the US.
Posted: Wed Apr 18, 2007 8:50 pm
by RobertGonzalez
date takes a unix time stamp. You would need to do something like:
Code: Select all
<?php
while ($row = mysql_fetch_array($result))
{
echo date('m-d-y', strtotime($row['my_date_field']));
}
?>
Posted: Thu Apr 19, 2007 2:23 am
by aaronhall
Isn't this a storage issue and not a retrieval issue?
Posted: Thu Apr 19, 2007 3:44 am
by onion2k
If you're formatting the date coming from a MySQL database table it's better to use MySQL's built in DATE_FORMAT function.
Code: Select all
SELECT DATE_FORMAT(`myTable`.`date_column`,"%d-$m-%Y") as `formatted_date` FROM `myTable` ORDER BY `myTable`.`date_column` DESC
Posted: Thu Apr 19, 2007 4:14 am
by stereofrog
onion2k wrote:If you're formatting the date coming from a MySQL database table it's better to use MySQL's built in DATE_FORMAT function.
I disagree. Displaying date is a presentation issue and therefore goes to View, while SQL statement are part of Model. Injecting presentation logic into Model is usually a bad idea.