2

I have the date string in the following way :

input:

$date = "Thu Jul 12 2012 11:03:36 GMT 0";

How do i remove the last words, starting from 'GMT' using regex.

output:

Thu Jul 12 2012 11:03:36

5 Answers 5

4

try this

$newdate = preg_replace("/GMT(.*)/i", "", $date)
Sign up to request clarification or add additional context in comments.

Comments

3
$result = preg_replace('~\s+GMT.*$~', '', $date);

Comments

0

Try this,

$newdate = preg_replace('\sGMT(.*)', '', $date);

Comments

0

Use DateTime object

$i = 'Thu Jul 12 2012 11:03:36 GMT 0';
$d = DateTime::createFromFormat('D M d Y H:i:s * *', $i);
echo $d->format('Y-m-d H:i:s'); # or whatever you need

Comments

0

One way would to to use preg_replace and use a pattern (30 Minute Regex Tutorial).

<?php
    $string = 'Thu Jul 12 2012 11:03:36 GMT 0';
    $pattern = '/GMT [0-9]*/';
    $replacement = ' ';
    echo preg_replace($pattern, $replacement, $string);
?>

Output

Thu Jul 12 2012 11:03:36 

or explode $string = explode('GMT', $string);

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.