php code not converting gmt to ist properly - php

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

Related

How to convert GMT time to IST?

Here I am getting GMT Time as follow.
1520489880
Now I am getting This as
$myfinaldate = gmdate("d M H:i", $strtoDate);
So it will returns me GMT time. Now i want to convet it to IST(indian standard time).
where I stuck when I try
//Set timezone to india
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
But this returns me current time not the time that I get in strtotime
You need to pass the timestamp:
date_default_timezone_set('Asia/Kolkata');
echo date('d M H:i', $strToDate); // where $strToDate is 1520489880
Would produce the following value:
08 Mar 11:48
You can convert GMT time to IST time using following ways :
1.
echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));
2.
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
Changing default timezone is not such a good idea. Use DateTime class, any you don't need to worry about default timezone anymore...
$dt = new DateTime("#$strToDate");
$dt->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $dt->format('Y-m-d H:i:s');
demo

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

Transform date to valid format for PostgreSQL with PHP

I have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59

Php convert datetime from twitter feed

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

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

Categories