Date Formats

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pcatwood1
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 7:50 pm

Date Formats

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

strtotime is your friend

Code: Select all

$userinput = '04/18/2007';
$ts = strtotime($userinput);
echo date('Y-m-d', $ts);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Have you considered using a javascript date picker?

Burrito has recently developed a very promising one found here
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Date Formats

Post 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.
User avatar
bert4
Forum Newbie
Posts: 18
Joined: Wed Apr 18, 2007 9:44 am
Location: Bali, Indonesia

Post 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....
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
pcatwood1
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 7:50 pm

dateformat

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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']));
}
?>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Isn't this a storage issue and not a retrieval issue?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
Post Reply