0

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.

Laurent
  • 711
  • 2
  • 12
  • 30
  • 2
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – jrn Jun 30 '17 at 18:02

3 Answers3

2

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');
jrn
  • 2,640
  • 4
  • 29
  • 51
1

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

JazZ
  • 4,469
  • 2
  • 20
  • 40
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
1
use strtotime:

$date_event = 'Fri, 30 Jun 2017 16:27:44 +0000 (UTC)';
echo date('d.m.Y H:i:s',strtotime($date_event));
Vishal Varshney
  • 905
  • 6
  • 11