Date Formats
Moderator: General Moderators
Date Formats
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?
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?
strtotime is your friend
Code: Select all
$userinput = '04/18/2007';
$ts = strtotime($userinput);
echo date('Y-m-d', $ts);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Have you considered using a javascript date picker?
Burrito has recently developed a very promising one found here
Burrito has recently developed a very promising one found here
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Date Formats
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.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?
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.volka wrote:strtotime is your friend
dateformat
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.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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']));
}
?>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- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
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.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.