tags:

views:

114

answers:

4

I have a date in the following format

November 18, 2009, 3:00PM

How can i break that up so that i can store each value as its own variable?

such as...

$month //November
$day //18
$year //2009
$hour //03
$minute //00
$ampm //PM

---------------Solution----------------

$startdate = "November 18, 2009, 3:00PM";

list($month,$day,$year,$time) = preg_split('/[ ,]/',$startdate,false,PREG_SPLIT_NO_EMPTY);
preg_match('/([0-9]+):([0-9]+)([AP]M)/',$time,$timeparts);
list($time,$hour,$minute,$ampm) = $timeparts;
$curdate = date_parse($startdate);

$month returns the "November", but in my specific application i needed the number, so i used the date_parse function to do that.

$current_month = $curdate[month];
$month // November
$day // 18
$year // 2009
$hour // 3
$minute // 00
$ampm // PM
+2  A: 

Convert your date into a timestamp, then with the timestamp you can easily get your parts. An other way is using a regular expression.

Ben Fransen
-1 for regex for dates. there are so many better php ways to do this.
tharkun
True, I'm only mentioning options. I mentioned the regex as last, thereby I kinda mean it's not my favorite option. I prefer the datefunction ofcourse.
Ben Fransen
with specific requirements regex is a good option. albeit not _the_ best option.
Peter Lindqvist
+1 for not giving him the code ;)
stereofrog
It's more because of the sort-of lazyness for not using Google for these situations.. This is widely discussed and there are many, many examples available...
Ben Fransen
+3  A: 

Use the 'date_parse' (http://nl2.php.net/manual/en/function.date-parse.php) function. It returns an array with the parsed items:

Array
(
    [year] => 2006
    [month] => 12
    [day] => 12
    [hour] => 10
    [minute] => 0
    [second] => 0
    [fraction] => 0.5
    [warning_count] => 0
    [warnings] => Array()
    [error_count] => 0
    [errors] => Array()
    [is_localtime] => 
)
TheGrandWazoo
how would i handle the ampm part?
Patrick
date_parse will return the hour on a 24h basis.
tharkun
The AM / PM part can be parsed with the 'date_parse_from_format'-function. It accepts a format used by all the date functions. These formats can contain AM/PM specification. See the details here: http://nl2.php.net/manual/en/function.date-parse-from-format.php
TheGrandWazoo
actually that function doesnt seem to work on my version of php...weird
Patrick
Or you can extract the AM/PM information separately with the A or a formatting options.
tharkun
+1  A: 
$str = "November 18, 2009, 3:00PM";
list($month,$day,$year,$time) = preg_split('/[ ,]/',$str,false,PREG_SPLIT_NO_EMPTY);
preg_match('/([0-9]+):([0-9]+)([AP]M)/',$time,$timeparts);
list($time,$hour,$minute,$ampm) = $timeparts;

echo "\$month  $month\n";
echo "\$day    $day\n";
echo "\$year   $year\n";
echo "\$hour   $hour\n";
echo "\$minute $minute\n";
echo "\$ampm   $ampm\n";

Output

$month  November
$day    18
$year   2009
$hour   3
$minute 00
$ampm   PM
Peter Lindqvist
this was the easiest option of all. You win! Thanks for the help and time everyone.
Patrick
Yes, it was the closest match to what you seemed to want, but the other suggestions have credit for being, more flexible, safer and so on.
Peter Lindqvist
+1  A: 

More complex solution. If your dates may be in the different standards you can use date() function (http://php.net/manual/en/function.date.php) + strtotime() function (http://php.net/manual/en/function.strtotime.php), which parse string and returns the unix timestamp.

For example, if you want to get a year from your date string you could write next code:

$date = 'November 18, 2009, 3:00PM';

$year = date('Y', strtotime($date));

Or, if you want to know how much days in the month in date you get, you could write such code:

$date = 'November 18, 2009, 3:00PM';

$num_of_days = date('t', strtotime($date));

't' returns the number of days in the given month.

Sergey Kuznetsov