Convert int-date to RSS PubDate RFC 822 PHP - php

I have a question that is making me crazy,
My Task is to parse a date from an API and transform it to RFC 822 format, because the feed that is coming out gets an validation error
the date from the API looks like this :
<review created="2012-10-23 14:51:12.0">
I have one Date in the description made via substr
$xtime = substr($review["created"], 0, 16);
$jahr = substr($xtime,0,4);
$mon = substr($xtime,5,2);
$tag = substr($xtime,8,2);
$datneu = $tag.'.'.$mon.'.'.$jahr;
this date will be rendered like :
23.10.2012
For the pubdate I made
$xtime = substr($review["created"], 0, 16);
$xxl = $pubtime . " GMT";
rendered like :
2012-10-23 14:51:12 GMT
And W3C feed validator says it´s not validate because pubDate is not in RFC 822 form
Sorry
This feed does not validate.
line 10, column 38: pubDate must be an RFC-822 date-time: 2012-10-29 11:51:23 GMT (5 occurrences) [help]
<pubDate>2012-10-29 11:51:23 GMT</pubDate>
and it needs to look like :
<pubDate>Wed, 02 Oct 2002 13:00:00 GMT</pubDate>
i can imagine a hacky solution for expressing sth like "if (month = 01){ actualmonth = Jan}" but i really don´t know how to do same with the days,
Also i´m not too comfortable with PHP but I need to solve this asap.
Hope you can help me, there must be a solution i didnt found at similiar questions
regards John

Have a look at DateTime::createFromFormat() or date_create_from_format.
http://fr2.php.net/manual/en/datetime.createfromformat.php
<?php
$date = date_create_from_format('Y-m-d H:i:s', '2012-10-23 14:51:12.0');
echo date_format($date, 'D, d M Y H:i:s');
?>
Have a look at the possible date formats
http://fr2.php.net/manual/en/function.date.php
EDIT: fixed

Hi in PHP you should look at this: function date

yeaaaaah that worked for me, amazing!
for others the exact solution for my Case was
$before = "2012-10-23 14:51:12.0";
$timetwo = substr($before, 0, 16);
$timethree = date_create_from_format('Y-m-d H:i:s', $timetwo);
$timefinal = date_format(timethree, 'D, d M Y H:i:s');
$after = $timefinal . ' GMT' ;
$after = "Mon, 23 Oct 2012 14:51:12 GMT";
thanks a lot for the quick answers you are awesome!

This worked for me....
date("r", strtotime($yourdate))
$yourdate was 2013-10-27 and I got Sun, 27 Oct 2013 00:00:00 +0000

Related

Using strtotime with TimeZone

I have a problem. I want to convert this DateTime: 2018-10-28 02:00:00 to a TimeStamp. Now the TimeStamp I am looking for is: 1540684800, but with my code I get this TimeStamp: 1540688400. I know it has something to do with my TimeZone, but I don't know how I can fix this.
I live in the Netherlands in Amsterdam.
Here is my code:
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = (strtotime($LoopDateTime)*1000);
Can someone help me?
The time zone identifier for Amsterdam is Europe/Amsterdam and 1540688400 is the correct timestamp. There's surely an online tool to check but you can also verify it from PHP itself:
$date = new DateTime("#1540688400");
$date->setDateTimeZone(new DateTimeZone('Europe/Amsterdam'));
echo $date->format('r'); // Sun, 28 Oct 2018 02:00:00 +0100
However your code is not robust because depends on the configured timezone. You can just set it explicitly in a number of ways, e.g.:
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = strtotime($LoopDateTime . ' Europe/Amsterdam') * 1000;
var_dump($search_key); // int(1540688400000)
Or:
date_default_timezone_set('Europe/Amsterdam');
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = strtotime($LoopDateTime) * 1000;
var_dump($search_key); // int(1540688400000)
P.S. If I'm not wrong Sunday 28 Oct 2018 02:00:00 +0100 is the exact moment when most Europe has just switched from CEST (+0200) to CET (+0100).

PHP create from format not formatting time correctly

I have this time string from sql 00:05:00 and I need a timestamp from it but can't seem to get it. I have also tried strtotime.
What am I doing wrong?
$NonBillable = '00:05:00';
$NonBillable = DateTime::createFromFormat('H:i:s', $NonBillable)->getTimestamp();
echo $NonBillable;
$NonBillable = DateTime::createFromFormat('H:i:s', '00:05:00');
echo $NonBillable->getTimestamp();
It results for me 1464480300 -> Sun, 29 May 2016 00:05:00 GMT because no date has been given, it uses today.
So a simple solution to this would be:
echo $NonBillable->getTimestamp() - time();

String Replace Date & Time

a line of my XML looks like this:
<observation_time_rfc822>Thu, 09 Oct 2014 22:59:16 +0200</observation_time_rfc822>
I grab it and give it out:
$ob_time= $xml->observation_time_rfc822;
echo $ob_time;
The output looks like this:
Thu, 09 Oct 2014 22:59:16 +0200
But what I need should look like this (yes, the funny '%3A' replaces ':')
2014-10-09+22%3A59%3A16
I think string replace can do this, please someone can help me to find out!
Thank you!
Edit: Use #Ghost's solution, it correctly handles the timezone offset.
First you need to reformat your date. You do this by parsing it with strtotime and formatting it with the date function. Those "funny %3A replaces" are actually URL-encoded characters:
$date = date('Y-m-d H:i:s', strtotime($ob_time));
$date = urlencode($date); // 2014-10-09+20%3A59%3A16
You could use DateTime class in this case, then use urlencode():
Example:
$ob_time = (string) $xml->observation_time_rfc822;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $ob_time);
$real_date = $date->format('Y-m-d H:i:s');
echo urlencode($real_date); // 2014-10-09+22%3A59%3A16

PHP Timestamp into DateTime

Do you know how I can convert this to a strtotime, or a similar type of value to pass into the DateTime object?
The date I have:
Mon, 12 Dec 2011 21:17:52 +0000
What I've tried:
$time = substr($item->pubDate, -14);
$date = substr($item->pubDate, 0, strlen($time));
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');
The above is not correct. If I loop through a lot of different dates its all the same date.
You don't need to turn the string into a timestamp in order to create the DateTime object (in fact, its constructor doesn't even allow you to do this, as you can tell). You can simply feed your date string into the DateTime constructor as-is:
// Assuming $item->pubDate is "Mon, 12 Dec 2011 21:17:52 +0000"
$dt = new DateTime($item->pubDate);
That being said, if you do have a timestamp that you wish to use instead of a string, you can do so using DateTime::setTimestamp():
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);
Edit (2014-05-07):
I actually wasn't aware of this at the time, but the DateTime constructor does support creating instances directly from timestamps. According to this documentation, all you need to do is prepend the timestamp with an # character:
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime('#' . $timestamp);
While #drrcknlsn is correct to assert there are multiple ways to convert a time string to a datatime, it's important to realize that these different ways don't deal with timezones in the same way.
Option 1 : DateTime('#' . $timestamp)
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
The strtotime bit eliminates the time zone information, and the date_create function assumes GMT.
As such, the output will be the following, no matter which server I run it on :
2011-12-12T13:17:52+00:00
Option 2 : date_create()->setTimestamp($timestamp)
Consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800')), 'c');
You might expect this to produce the same output. However, if I execute this code from a Belgian server, I get the following output :
2011-12-12T14:17:52+01:00
Unlike the date_create function, the setTimestamp method assumes the time zone of the server (CET in my case) rather than GMT.
Explicitly setting your time zone
If you want to make sure your output matches the time zone of your input, it's best to set it explicitly.
Consider the following code :
date_format(date_create('#'. strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Now, also consider the following code :
date_format(date_create()->setTimestamp(strtotime('Mon, 12 Dec 2011 21:17:52 +0800'))->setTimezone(new DateTimeZone('Asia/Hong_Kong')), 'c')
Because we explicitly set the time zone of the output to match that of the input, both will create the same (correct) output :
2011-12-12T21:17:52+08:00
Probably the simplest solution is just:
DateTime::createFromFormat('U', $timeStamp);
Where 'U' means Unix epoch. See docs: http://php.net/manual/en/datetime.createfromformat.php
This is my solution:
function changeDateTimezone($date, $from='UTC', $to='Asia/Tehran', $targetFormat="Y-m-d H:i:s") {
$date = new DateTime($date, new DateTimeZone($from));
$date->setTimeZone(new DateTimeZone($to));
return $date->format($targetFormat);
}

Send PHP date to JavaScript date format

I want to pass my PHP server time to my JavaScript file.
PHP Code:
date_default_timezone_set('Australia/Perth');
echo date("r");
JavaScript:
$.get('time.php', function(data) {
today = new Date(data);
closing = new Date(data);
});
The PHP code returns Sun, 18 Mar 2012 12:01:23 +0800 which is correct time for Australia/Perth. But this returns an invalid JavaScript date object.
When I try to convert it to timestamp like:
echo strtotime(date("r"));
I get the JavaScript date Sun Mar 18 2012 04:03:14 GMT+0000 (WET) (this is the value of today js var)
If I use:
echo gmstrftime('%s');
I get: Sat Mar 17 2012 20:04:30 GMT+0000 (WET).
Can anyone please help me out?
The PHP code in Luna's answer with echo date isn't exactly like JavaScript code. This will mimic the JavaScript code exactly:
echo date('D M d Y H:i:s O');
You could also just leave the PHP code as it is and parse the date using JavaScript:
var date = new Date(Date.parse(DATE));
Then even things like this would work:
new Date(Date.parse('11 March 2017'));
Which outputs via a console log (GMT+1000 is because I am in Australia):
Sat Mar 11 2017 00:00:00 GMT+1000
More information is here: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
$.get('time.php', function(data) {
today = new Date(data);
closing = new Date(data);
});
What was the purpose of multiplying the string by 1000? That operation doesn't make sense.
This PHP will work for that.
echo date('D, d M y H:i:s')." +0000";
date('D M d Y H:i:s O')
It won't work if your current locale isn't English.
A better alternative is to use:
new Date(<? echo date("Y, n - 1, d, H, i, s") ?>)
Here is an example with the DateTime object:
PHP code (works on PHP 5.3 or later)
$serverDate = new \DateTime('NOW');
// If you want to set a different time zone
// $serverDate = new \DateTime('NOW', new \DateTimeZone('Australia/Perth'));
echo $serverDate->format(DATE_ATOM);
JavaScript code
$.get('time.php', function(data) {
var serverDate = new Date(data);
});
A good way is timestamp:
echo $data = time()*1000;
echo '
<div id="setxDatetime">The current server time is: </div>
<script type="text/javascript">
var x = document.getElementById("setxDatetime");
x.innerHTML = x.innerHTML + new Date(' . $data . ');
</script>
';
1381324693000
The current server time is: Wed Oct 09 2013 16:18:13 GMT+0300 (GTB Standard Time)
There might be better solutions, but this one did the trick for me. The key issue is that JavaScript uses months 0-11, while PHP uses 1-12 as mentioned previously.
function convert_date_php_js($date) {
$converted_date = date("Y", strtotime($date)) . ', ' .
(date("n", strtotime($date))-1) . ', ' .
date("j", strtotime($date));
return $converted_date;
}
$priceDate = "2016-09-14";
$d = convert_date_php_js($priceDate);
// Returns 2016, 8, 14
It is very simple:
new Date("<?= date('Y/m/d H:i:s'); ?>");

Categories