Convert to DATETIME from "Fri, 10 Jun 2016 14:30:38 -0500" - php

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));

Related

PHP Put a Date Stamp and a Time Stamp together

I got two different timestamps in milliseconds. One is for the chosen DATE. One for the chosen time. I need now to send this to an API and I have to put the DATE and the TIME together. How can I achieve this?
$time = $request -> newTime1; //newTime1 is 1452800524000 which is: Thu Jan 14 2016 20:42:04 GMT+0100 (CET)
$datum = $request -> newDate1; //newDate1 is 1453676400000 which is: Mon Jan 25 2016 00:00:00 GMT+0100 (CET)
$dateformatted = strtotime($datum);
$timeformatted = date ("H:i:s",strtotime($time));
$combinedDT = strtotime("$dateformatted $timeformatted");
$event_start = $combinedDT;
$event_end = $event_start + 3600;
//I want 25.1.2016 20:42
<?php
date_default_timezone_set('CET');
$time = 'Thu Jan 14 2016 20:42:04 GMT+0100 (CET)';
$datum = 'Mon Jan 25 2016 00:00:00 GMT+0100 (CET)';
$t = strtotime($time);
$d = strtotime($datum);
$combined = strftime('%d.', $d) . intval(strftime('%m', $d)) . strftime('.%Y', $d)
. strftime(' %H:%M', $t);
echo $combined;
output
25.1.2016 20:42
I think the main problem is that the time input is multiplied by 100 (not in seconds).
After you solve it the most simple way is too use two date functions
$time = 14528005240;
$datum = 1453676400;
echo $date = date('d.m.Y',$datum).' '.date('H:i',$time);

DateTime format in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
how can I format datetime in php from: Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time) to: 2014-12-09 12:00?
Thank you.
Problem solved. I did it by myself:
$datep = $_POST["datetimepicker"];
$placeholders = array('(FLE', 'Standard', 'Time)');
$vals = array('', '', '');
$str = str_replace($placeholders, $vals, $datep);
$newDate = date('Y/m/d H:i', strtotime($str));
echo $newDate;
One method is to use the datetime class:
$dateTime = new DateTime($yourDateTimeVariable);
$dateTime = $dateTime->format('Y-m-d H:i');
echo $dateTime;
Try this -
$date = "Tue Dec 09 2014 12:00:00 GMT+0200";
$newDate = date('Y-m-d H:i', strtotime($date));
You can use strototime combined with date. Something like
$date = "Tue Dec 09 2014 12:00:00 GMT+0200 (FLE Standard Time)";
$format = date("Y-m-d H:i", strtotime($date));

Want to convert GMT date time to IST Date Time

i want to convert GMT date time to IST Date Time for that purpose i have tried below code but not getting desired result.
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();
date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");
$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");
date_default_timezone_set($system_timezone);
$diff = (strtotime($local) - strtotime($gmt));
$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("m-d-Y H:i:s");
return $timestamp;
}
$ISTtime=ConvertGMTToLocalTimezone('Tue, 17 Dec 2013 07:23:56 +0000','Asia/Calcutta');
echo $ISTtime;
Result: 12-17-2013 18:34:02
What i am doing wrong?
Why don't you simply do this:
$timestamp = strtotime('Tue, 17 Dec 2013 07:23:56 +0000');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
output
Tue, 17 Dec 2013 12:53:56 +0530
Can you try this, you can use $new = new DateTimeZone('Asia/Kolkata');//IST
$new = new DateTimeZone('Asia/Kolkata');//IST
$date = new DateTime(gmdate("m/d/Y H:i:s"), 'Tue, 17 Dec 2013 07:23:56 +0000');
$date->setTimezone($new);
echo $date->format('m-d-Y H:i:s');
This is a trivial exercise using the dateTime classes:-
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$date = new \DateTime('Tue, 17 Dec 2013 07:23:56 +0000');
$date->setTimeZone(new \DateTimezone($timezoneRequired));
return $date->format('D, d M Y H:i:s O');
}
$ISTtime=ConvertGMTToLocalTimezone('Tue, 17 Dec 2013 07:23:56 +0000','Asia/Calcutta');
echo $ISTtime;
Output:-
Tue, 17 Dec 2013 12:53:56 +0530
See it working
If you have PHP >= 5.4 this will work:-
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
return (new \DateTime('Tue, 17 Dec 2013 07:23:56 +0000'))->setTimeZone(new \DateTimezone($timezoneRequired))->format('D, d M Y H:i:s O');
}

Date formatting in php, unexpected result

The Script Below:
<?php
$postedDate = "Sat May 11 2013 20:36:24 GMT-0700 (PDT)";
$date = date('Y-m-d H:i:s', strtotime($postedDate));
echo json_encode($date);
?>
Returns the value:
"2013-05-12 03:36:24"
I expect and want it to return the value:
"2013-05-11 20:36:24"
Any ideas as to what's going on?
Remove GMT - 0700 (PDT) and try you will get your desired result
$postedDate = "Sat May 11 2013 20:36:24";
$date = date('Y-m-d H:i:s', strtotime($postedDate));
echo json_encode($date);
Codepad
Try this:
$str = 'Sat May 11 2013 20:36:24 GMT-0700 (PDT)';
$date = date('Y-m-d H:i:s',strtotime('-7 hours',strtotime($str)));
echo json_encode($date);
Or set timezone properly using date_default_timezone_set.
http://php.net/manual/ja/function.date-default-timezone-set.php

remove the words from string using regex in php

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);

Categories