1

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

3
  • The usual answer is 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"")? Commented Feb 11, 2012 at 17:45
  • The string can change between this two formats: Tuesday, February 21 at 7:30am at Plano B and Tuesday, February 21 at 7:30am without the last "at..." Commented Feb 11, 2012 at 17:47
  • @Pekka, surely one doesn't need an "at", there's "am" and "pm" to hook any regex off. Commented Feb 11, 2012 at 17:49

3 Answers 3

2
$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]);
Sign up to request clarification or add additional context in comments.

7 Comments

bugs when the location is missing: "Tuesday, February 21 at 7:30am"
it can be 'Plano B', empty or any other string.
with your example above the output is alway Date: Thu 01 Jan 01:00 with all the date that i've tried :(
after the 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 regex
Ehm, I think it's actually missing the year to parse via strtotime. Is there a way to add the year to the string? it is not a full date now..
|
1

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

Comments

0
// Strip the last at, if its not a time
if(!preg_match("/at [0-9]+:[0-9]+[ap]m$/", $str)) {
  $str = preg_replace("/at [^0-9].*/","",$str);
}

// Then convert to time
$time = strtotime($str);

// Then output in specified format
echo strtoupper(date("D d M h:i", $time));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.