Preg_split to be used to process date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Preg_split to be used to process date

Post by ViserExcizer »

January 1, 2008

June 25, 2009 - 8:24 am

I need to break down the first date into 3 arrays, Month, Date, year.
And the second one the same way, but the time could be ignored.

This is what i've done so far

Code: Select all

 
$datenow = mktime (date("h"),date("i"),date("s"),date("m"),date("d"),date("Y"));
list($year,$month,$day,$hour,$minutes,$seconds) = preg_split('/[ :-]+/', $datenow);
this processes the mysql datetime format, (0000-00-00 00:00:00) properly, but i dont know how to make a regex to process a format that looks like this (June 25, 2009) or (June 25, 2009 - 8:24 am). So i basically need help with this regex. Someone recommended using strtotime() but that only returns a date if true/false. I need a preg_split with regex. thanks.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Preg_split to be used to process date

Post by McInfo »

Code: Select all

<?php
header('Content-Type: text/plain');
//$str = 'June 25, 2009';
$str = 'June 25, 2009 - 8:24 am';
preg_match('/([a-z]+) ([0-9]{1,2}), ([0-9]{4})( - ([0-9]{1,2}):([0-9]{2}) (am|pm)){0,1}/i', $str, $matches);
print_r($matches);
?>
"June 25, 2009"

Code: Select all

Array
(
    [0] => June 25, 2009
    [1] => June
    [2] => 25
    [3] => 2009
)
"June 25, 2009 - 8:24 am"

Code: Select all

Array
(
    [0] => June 25, 2009 - 8:24 am
    [1] => June
    [2] => 25
    [3] => 2009
    [4] =>  - 8:24 am
    [5] => 8
    [6] => 24
    [7] => am
)
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:55 pm, edited 1 time in total.
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Re: Preg_split to be used to process date

Post by ViserExcizer »

Okay, thanks,

but how do i echo the array?

like

$array[1] = $month;
$array[2] = $date;
$array[3] = $year;

echo $month . $ date . $year ;
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Preg_split to be used to process date

Post by McInfo »

You can use list() the same way you did with the array that was returned by preg_split() in your first post.

Code: Select all

list(, $month, $day, $year) = $matches;
echo "<ul><li>Year: $year</li><li>Month: $month</li><li>Day: $day</li></ul>";
Or you can access each index individually.

Code: Select all

$year  = $matches[3];
$month = $matches[1];
$day   = $matches[2];
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:56 pm, edited 1 time in total.
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Re: Preg_split to be used to process date

Post by ViserExcizer »

thanks very, i didnt think that through. thanks alot for your help.
Post Reply