Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL PHP Date conversion problem

Status
Not open for further replies.

alohaaaron

Programmer
Mar 27, 2008
80
US
Hi, I'm trying to convert a date of this format:
12-5-2008 (m d Y) to 2008-12-5 (Y m d) but when I do the conversion it doesn't display the correct date.

I start with:
$my_date = '12-5-2008';
$my_time = strtotime($my_date);

echo date('Y-m-d',$my_time);
'This displays 2017-10-29 for some reason???

echo date('m-d-Y',$my_time);
'This displays 10-29-2017 for some reason???

If I start with $my_date = '2008-12-5'; //(Y-m-d)
Then the conversion works fine.

 
remember that your start date in that form has to be yyyy-mm-dd. try with that as the input and see what you get.
 
Hi, Thank for your reply. The problem is I have users entering input in m-d-Y format. How do I get it to work if the input is not in Y-m-d format?
 
here are the input rules

m-d-y should work ordinarily. it may be that you have to replace the dashes with slashes.

Code:
$my_date = str_replace ('-', '/', $my_date);

but if you can always change the order of things around without using date functions

assume an input of 12-5-2008

Code:
$start = '12-5-2008';
list ($month, $day, $year) = explode ('-', $start);
echo "$year-$month-$day";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top