Jump to content

Archived

This topic is now archived and is closed to further replies.

Zharvek

Date Converstion

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.