Jump to content


Photo

Date Converstion


  • Please log in to reply
3 replies to this topic

#1 Zharvek

Zharvek
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 March 2006 - 06:50 PM

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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 March 2006 - 07:08 PM

You should look at the date() and strtotime() functions.
<?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;
?>

Ken

#3 Zharvek

Zharvek
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 March 2006 - 07:28 PM

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.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 March 2006 - 08:13 PM

[!--quoteo(post=356734:date=Mar 20 2006, 02:28 PM:name=Zharvek)--][div class=\'quotetop\']QUOTE(Zharvek @ Mar 20 2006, 02:28 PM) View Post[/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:
$date = "2006-03-20";
list($year, $month, $day) = explode("-", $date);
echo "$year<br />\n";
echo "$month<br />\n";
echo "$day<br />\n";

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users