using date()

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
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

using date()

Post by QbertsBrother »

hello all

i have a date stored in a mysql database and when i do a query and select the date and use this

Code: Select all

date("m.d.y", mydatefield)
it shows all the dates as 12.31.69

i dont know why it is doing this. anyone have any suggestions on how to get the formating correct?

thanks
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

sounds like your doing a date on the number -1 check the value of mydatefield first..
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Post by QbertsBrother »

mydatefield is a Date datatype in a mysql database.

the value of mydatefield looks like this but with different dates.

2007-12-01
2007-12-02
2007-12-03
2007-12-12

what ever the date is that the record was entered.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

try

Code: Select all

date("d.m.y", mydatefield)
just incase..
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Post by QbertsBrother »

d.m.y produces 31.12.69
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

I've had so much trouble with date() back a few months ago, so I was just happy to get it working. It's probably not the prettiest code, but it works for me.

Code: Select all

$updatedDate = date('Y-m-d', strtotime("", strtotime($date)));
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Post by QbertsBrother »

Code: Select all

$updatedDate = date('Y-m-d', strtotime("", strtotime($date)));
still produces 12.31.69

the funny if i just use this

Code: Select all

date("m.d.y")
it produces todays date correctly.

do i need to change the datatype of the data to something else?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would suggest using the MySQL DATE_FORMAT() function instead of messing around with dates in PHP.
(#10850)
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Post by QbertsBrother »

thank you very much for the help everyone.

the date_format worked great.

thanks
Post Reply