Zharvek Posted March 20, 2006 Share Posted March 20, 2006 Hello, I have a odd situation in PHP where I need to convert an date to a different format...I have a date like, 2006-03-19 16:42:01 and I would like to "convert" it into something like...$year = "2006";$month = "03";$day = "19";I don't need the time at all. I just need the year, month, and day from that long string into some variables so I can pull data from the MySQL server correctly.I was guessing that I could use a trim of some sort, but is this the best way?I cannot change the long date format above because it is already in wide spread use on the site.Thank you in advance to anyone who can help. Quote Link to comment https://forums.phpfreaks.com/topic/5361-date-converstion/ Share on other sites More sharing options...
kenrbnsn Posted March 20, 2006 Share Posted March 20, 2006 You should look at the date() and strtotime() functions.[code]<?php$long_date_time = "2006-03-19 16:42:01";$short_date = date('Y-m-d',strtotime($long_date_time));echo $long_date_time.'<br>'.$short_date;?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5361-date-converstion/#findComment-19100 Share on other sites More sharing options...
Zharvek Posted March 20, 2006 Author Share Posted March 20, 2006 Thank you very much, that solves a big part of the problem. Now that the time is removed, I can work on getting the parts into their variables correctly. Quote Link to comment https://forums.phpfreaks.com/topic/5361-date-converstion/#findComment-19115 Share on other sites More sharing options...
obsidian Posted March 20, 2006 Share Posted March 20, 2006 [!--quoteo(post=356734:date=Mar 20 2006, 02:28 PM:name=Zharvek)--][div class=\'quotetop\']QUOTE(Zharvek @ Mar 20 2006, 02:28 PM) [snapback]356734[/snapback][/div][div class=\'quotemain\'][!--quotec--]Thank you very much, that solves a big part of the problem. Now that the time is removed, I can work on getting the parts into their variables correctly.[/quote]easiest way to do that that i know of is like so:[code]$date = "2006-03-20";list($year, $month, $day) = explode("-", $date);echo "$year<br />\n";echo "$month<br />\n";echo "$day<br />\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5361-date-converstion/#findComment-19125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.