Php convert datetime from twitter feed - php

Hey all i have this date for one of my twitter feeds:
Tue Apr 16 17:39:59 +0000 2013
Problem is that this code below does not seem to format it?
$date = new DateTime('Tue Apr 16 17:39:59 +0000 2013');
echo $date->format('Y-m-d H:i:s');
It just shows up as a blank page.
What could i be doing wrong?

Try:
$date = DateTime::createFromFormat('D M j H:i:s O Y', 'Tue Apr 16 17:39:59 +0000 2013');
echo $date->format('Y-m-d H:i:s');
See it in action

Try this
$date = 'Tue Apr 16 17:39:59 +0000 2013';
echo gmdate('Y-m-d H:i:s', strtotime($date));

i've tested out your code on a console app with php and works fine.
I think that maybe you have a problem with the code that display the page in the browser.
Other thing that you can try is to inspect the source of the page and see if is showing there.
BTW, i'm using PHP 5.4.7

Related

PHP : reformat a date in

Good day,
I have the following date that I get from my imap_fetch_overview PHP function :
$overview = imap_fetch_overview($inbox, $email_number, 0);
...
$date_event = $overview[0]->date;
This outputs a date like 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)'
In the meantime I have set my default timezone as following :
date_default_timezone_set('Europe/Brussels');
What I would like now is to be able to retrieve my $date_event in a format dd.mm.yyyy HH:mm:ss (local time, in my case 30.06.2017 18:27:44).
I have therefore tried :
$date_event_formated = $date_event('dd.mm.YYYY HH:ii:ss');
When calling 'echo $date_event_formated;', the only thing I get is a Fatal error.
So sorry if the question might sound silly, but I don't really understand what I am doing wrong here? Before bothering you, I looked at the reference website http://php.net/manual/fr/function.date.php but I may have missed something.
Thanks very much for your time and appreciated help.
While there is nothing wrong with Jakub's answer, using php 5.3 and up you can also use DateTime::createFromFormat, using an exact mask to parse the incoming date-string.
$oldDate = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
$date = DateTime::createFromFormat('D, d M Y H:i:s T e', $oldDate);
echo $date->format('Y-m-d');
strtotime() is able to handle the format that you have and convert it into timestamp. Then you can use it to format date in whatever format you want with date() function:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));
//outputs: 30.06.2017 18:27:44
Here's working example:
https://3v4l.org/obfNB
use strtotime:
$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));

php code not converting gmt to ist properly

This is a very small issue yet i am not able to solve it
i wish to convert gmt to ist through php, for this i have the following code
$timestamp = strtotime('Tue, 17 Dec 2013 07:23:56 +0000');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
the code is working properly, now i have an array through which i have fetched time which is in this format
10-Dec-2015 10:45:02 +0000
i have stored in a variable $maildate
Now when i am placing this variable in the code like this
$timestamp = strtotime('$maildate');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
i am getting this date and time Thu, 01 Jan 1970 05:30:00 +0530
can anyone please tell where i am going wrong
You need to get rid of quotes and update your code from
$timestamp = strtotime('$maildate');
^^ ^^
into
$timestamp = strtotime($maildate);
//^^ ^^ Removed quotes
Instead you can use DateTime class like as
$date = DateTime::createFromFormat('d-M-Y H:i:s P',$var);
$date->setTimeZone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('r');
Check out this
$timestamp = strtotime($maildate);
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);

how can I format my date in php to have the query new Date() format?

I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php

Date is not stored correctly in a cookie

I stored this date in a cookie:
Tue Jun 26 2012 15:38:41 GMT+0200 (CEST)
and now I need to print it in this form:
06/26/2012 15:38
So I write this code:
echo $_COOKIE['date'];
echo date('m/d/Y g:i A', strtotime($_COOKIE['date']));
but I get this:
Tue Jun 26 2012 15:38:41 GMT 0200 (CEST)
12/31/1969 7:00 PM
instead of:
06/26/2012 15:38
Why?
strtotime does not seem to understand the date format, you can try to parse it with DateTime::createFromFormat or you can try to store it in another format
Perhaps its easier to store the timestamp in the cookie and not the date string.
$_COOKIE['date'] = time();
and then read it direct to the date function.
i have tried this:
<?php
setcookie('date', time());
var_dump($_COOKIE);
echo date('m/d/Y g:i A', $_COOKIE['date']);
?>
and the result is: 06/25/2012 6:13 PM
Because strtotime is not recognizing the date format you are using.

Converting between timezones in PHP

I am converting this time and date:
Thu, 31 Mar 2011 02:05:59 GMT
To the following time and date format:
Monday March 28 2011 4:48:02 PM
I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set() but I am not sure how to implement that into the code I have below.
$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))
I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));
See this list for available timezones that get passed into the function

Categories