Page 1 of 1
Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 6:09 am
by ViserExcizer
Hello, im having trouble splitting a date time format to perform later operations using mktime,
What Regex should i use to separate a datetime of 2009-01-30 04:57:18
so that i can store it in a list?
Code: Select all
$date = "2009-01-30 04:57:18";
list ($hour,$minute,$second,$month, $day, $year) = preg_split(" what should come here? ", $date);
$nudate= mktime ($hour,$minute,$second,$month,$day,$year);
I tried using /[\\/-]/ but it only separated the date and not the time, and will return error because there is a single white space between the date and the time.
So i need the complete regex for this that will put all six vars in the mktime format, help much appreciated.
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 6:34 am
by prometheuzz
There is probably some easier way to do this using PHP's built-in date-functions, but I don't know any PHP (except a bit of regex), so here's a way:
Code: Select all
$str = "2009-01-30 04:57:18";
preg_match('/(?P<Y>\d+)-(?P<M>\d+)-(?P<D>\d+) (?P<h>\d+):(?P<m>\d+):(?P<s>\d+)/', $str, $m);
echo "hr={$m['h']}, min={$m['m']}, sec={$m['s']}, mn={$m['M']}, day={$m['D']}, yr={$m['Y']}";
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 9:27 am
by ViserExcizer
this solution almost worked, but it gives a an error when i try to assign a variable to each value,
$month = '{$m['M']}';
$year = '={$m['Y']}';
and so on, is there a way to use regex preg-split instead? i got this regex, but it doesnt seem to work, and the mktime +/- operation gives a bizzare value.
$pat = "/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/";
I need to separate this '2009-1-30 11:34:56'
into this
$year = ' ';
$month =' ';
and so on, how to do it?
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 9:31 am
by papa
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 10:02 am
by prometheuzz
ViserExcizer wrote:...
I need to separate this '2009-1-30 11:34:56'
into this
$year = ' ';
$month =' ';
and so on, how to do it?
In your original post, you indicated that you wanted a different ordering after splitting the date. If this is not the case however, then you can simply do:
Code: Select all
$str = '2009-1-30 11:34:56';
list($year,$month,$day,$hour,$minutes,$seconds) = preg_split('/[ :-]+/', $str);
echo "$year,$month,$day,$hour,$minutes,$seconds";
or even:
Code: Select all
list($year,$month,$day,$hour,$minutes,$seconds) = preg_split('/\D+/', $str);
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 12:27 pm
by ViserExcizer
prometheuzz, thanks very much, the latest preg_split pattern you gave works great with the epoch funtion,
Re: Need to preg_split a DATETIME variable
Posted: Fri Jan 30, 2009 1:40 pm
by prometheuzz
ViserExcizer wrote:prometheuzz, thanks very much, the latest preg_split pattern you gave works great with the epoch funtion,
Good to hear it, and you're welcome.