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
try this
$newdate = preg_replace("/GMT(.*)/i", "", $date)
$result = preg_replace('~\s+GMT.*$~', '', $date);
Try this,
$newdate = preg_replace('\sGMT(.*)', '', $date);
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
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);
Related
I currently have the date in the format: Day, day month year 24hr with seconds and timezone. I want to get this into Y-m-d.
Current Date:
Fri, 23 Mar 2018 20:28:34 0000
Final Date:
2018-03-23
I am using PHP to do this. The date is hard coded so I am unable to call the date() function at all.
Another alternative:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = trim( preg_replace( "/[0-9\+]*$/", "", trim($str) ) );
echo date( "Y-m-d", strtotime($str) );
Output:
2018-03-23
You can try using this code
echo date( "Y-m-d", strtotime(preg_replace( "/[0-9]+$/", "", "Fri, 23 Mar 2018 20:28:34 0000")) );
This will output the same answer you required
You can use createFromFormat() to transform your date:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = preg_replace('~\s(\d{4})$~', '+$1', $str); // change '0000' to '+0000'
$d1 = DateTime::createFromFormat("D, d M Y H:i:s O", $str) ;
echo $d1->format("Y-m-d") ;
// or
// echo date("Y-m-d", strtotime($str)) ;
Outputs:
2018-03-23
Or, as pointed out by #Andreas, you could use substr() to remove timezone:
$str = "Fri, 23 Mar 2018 20:28:34 0000";
$str = substr($str, 0,-5) ;
echo date("Y-m-d", strtotime($str)) ;
If the date is hardcoded as you say, that means it will always have a three lettered day, a comma then the date, then time and timezone.
The length of the string should be the same always.
This can be used to an advantage.
$str = "Fri, 23 Mar 2018 20:28:34 0000";
Echo date("Y-m-d", strtotime(substr($str,5,11)));
Here I use only the 23 Mar 2018 in the strtotime and date function.
No need for regex or trimming or adding + or - to timezones.
I am trying to pull RSS data from a page and upload it to my database.
example:
<dc:date>Fri, 10 Jun 2016 14:30:38 -0500</dc:date>
the -0500 is the trouble.
what I usually do:
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = $dc1->date;
$pubDate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $pubDate1)));
Should I be defining $pubDate differently?
No need of replacing '-' with '/'. You can directly pass the $pubDate1
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = $dc1->date; // 'Fri, 10 Jun 2016 14:30:38 -0500'
$pubDate = date('Y-m-d H:i:s', strtotime($pubDate1));
OutPut :
2016-06-11 12:30:38
If all of your date have the last part, maybe this code works. but if the last part(-0500) appears in some of them it wont work:
$dc1 = $xml->channel->item[$i]->children($namespaces["dc"]);
$pubDate1 = explode(' ',$dc1->date);
// remove -0500
array_pop($pubDate1);
$pubDate1 = implode(' ',$pubDate1);
$pubDate = date('Y-m-d H:i:s', strtotime($pubDate1));
Hello I am tring to get date "8 January 2014" from given string.
Wednesday, 8 January 2014 at 17:40
It will be good if you can use preg_replace?
PHP strtotime is bad at understanding the word "at" so this code should work and not return 1970:
$date = date("d F Y", strtotime(str_replace('at ','',"Wednesday, 8 January 2014 at 17:40")));
echo $date;
You can change the format just as you want; just change the letter d F Y and the list of letter that returns different values can be found in the manual.
Just made solution with explode() function:
$str = "Wednesday, 8 January 2014 at 17:40";
$ex1 = explode(", ", $str);
$ex2 = explode(" at", $ex1[1]);
$value = $ex2[0];
echo $value;
The DateTime class makes your life simpler !
<?php
$date = DateTime::createFromFormat('l, j F Y \a\t G:i','Wednesday, 8 January 2014 at 17:40');
echo $date->format('j F Y'); //"prints" 8 January 2014
Try regex pattern like this:
$str = 'Wednesday, 8 January 2014 at 17:40';
$date = preg_replace('/\w+,\s([a-zA-Z0-9\s]+)\sat\s([0-9:]+)/i', '$1', $str);
echo $date;
Here is simple code to get 8 January 2014 for you :)
<?php
$txt = 'Wednesday, 8 January 2014 at 17:40';
preg_match('#(?<=,\s)\d{1,2}\s\w+\s\d{1,4}(?=\sat)#', $txt, $result);
?>
Is there a simple way to get the day from a very simple date string?
For example:
14 mar 2012
gets converted to:
Wed, 14 mar 2012
You should familiarize yourself with DateTime:
$str = "14 mar 2012";
$date = DateTime::createFromFormat("j M Y", $str);
$str = $date->format("D, j M Y");
$dateUnformated = '14 mar 2012';
$dateFormated = date("D, j M Y", strtotime($dateUnformated));
How can I covert this PHP date string:
Thu 19th Aug 2010 # 7:52PM
to this:
1282247549
Which is done by:
gmdate("D jS M Y # g:iA", $row['project_deadline'])
The timestamp is from the database with the stored time() function
You can parse that string into a UNIX timestamp with the strtotime function:
$str = 'Thu 19th Aug 2010 # 7:52PM';
$str = str_replace('#', '', $str);
$timestamp = strtotime($str);
If you have unstandard format, then you should use DateTime::createFromFormat :
$str = 'Thu 19th Aug 2010 # 7:52PM';
$dt = DateTime::createFromFormat('!D jS M Y # g:iA', $str);
echo $dt->getTimestamp();