I've the following string:
$str = "Tuesday, February 21 at 7:30am at Plano B";
The at Plano B is optional. I would like to convert it to: TUE 21 FEB 07:30
$str = "Tuesday, February 21 at 7:30am at Plano B";
$time = strtotime(trim(substr($str,0,(strrpos("at"))));
echo "Date: " . strtoupper(date('D d M H:i', $time));
What do you mean by "at Plano B is optional". Is it sometimes there, sometimes not?
Otherwise:
$str = "Tuesday, February 21 at 7:30am at Plano B";
preg_match("/[a-z]+, ([a-z]+ [0-9]{1,2}) at ([0-9]{1,2}:[0-9]{1,2}[am|pm])/i", $str, $match);
$time = strtotime($match[1] + ' ' + $match[2]);
echo "Date: " . strtoupper(date('D d M H:i', $time));
Is it always either "Plano B" or empty? or can it also be "Plano A" or something completely diffrent?
See here: http://regexr.com?2vvuj
But you are missing the year in the initial string, so can't parse as strtotime. Also you want output without am/pm.. Do you want to use 24 hour time?
This is not a pretty way, but without the year, i dont think we have much choice..
preg_match("/([a-z]+), ([a-z]+) ([0-9]{1,2}) at ([0-9]{1,2}:[0-9]{1,2})([am|pm])/i", $str, $match);
$day = substr($match[1], 0, 3);
$mon = substr($match[2], 0, 3);
echo strtoupper($day . " " . $match[3] . " " . $mon . " " . $match[4]);
"Tuesday, February 21 at 7:30am"preg_match add this: echo "<pre>".print_r($match,1)."</pre>"; to make sure the result is correct. I have not tested the code in PHP, only the regexstrtotime. Is there a way to add the year to the string? it is not a full date now..I'd like to propose a slightly different solution based on the not as oftenly used strptime. It uses a pre-defined format to parse the string.
Example:
<?php
// Specify a default timezone just in case one isn't set in php.ini.
date_default_timezone_set('America/Vancouver');
$str = "Tuesday, February 21 at 7:30am at Plano B";
if ($time = strptime($str, '%A, %B %e at %l:%M%P')) {
// This will default to the current year.
echo strtoupper(date('D d M H:i', mktime($time['tm_hour'], $time['tm_min'], 0, $time['tm_mday'], $time['tm_mon'])));
}
Output:
SUN 01 SEP 07:30
strtotime(), but the "at Plano B" is going to confuse it. Is there a common structure to every string (like, "every string will contain "at"")?