I have a timezone date that have been posted to some PHP script.
like (Tue Dec 24 2013 00:00:00 GMT+0200 (Egypt Standard Time).
Now all what i need is simply convert that date to match mysql date which in "Y-m-d" format.
Is there any direct function? How to do that?
DateTime() will handle this easily.
$dt = new DateTime('Tue Dec 24 2013 00:00:00 GMT+0200');
echo $dt->format('Y-m-d');
Related
I want to fetch the correct date as per the timezones.
ex. I have a time zone +5:30 from GMT. if GMT is 30 aug 2016 1:00 pm then for gmt+5:30 should give me 30 Aug 2016 6:30 pm however, adding timezones like that actually subtracts it rather adding.
I have this code:
$a=date('Y-m-d H:i:s');
which gives me 2016-08-30 07:36:01 as per GMT which is correct.
$b="+5:30";
$c=(strtotime($a.$b));
echo($c);
it gives me 30 Aug 2016 02:08:25 which is wrong I should get 30 Aug 2016 13:10:32.
What I mean is, if I am adding the timezone value it is getting subtracted and if I do same with -5:30 as timezone I get the correct result. Can somebody please suggest what am I doing wrong or how this should work actually.
Use DateTimeZone and DateTime objects to make it more obvious while working with timezone offsets:
$a = "2016-08-30 07:36:01";
$b = "+5:30";
$gmtTz = new \DateTimeZone("GMT");
$offset = new \DateTimeZone($b);
$dt = new \DateTime($a, $gmtTz);
$dt->setTimezone($offset);
echo $dt->format("Y-m-d H:i:s"); // "016-08-30 13:06:01"
Try this:
Solution 1 :
$minutesToBeAdded = 330;
$currentDate = date('Y-m-d H:i:s');
$currentTime = new DateTime($currentDate);
$currentTime->add(new DateInterval('PT' . $minutesToBeAdded . 'M'));
$newTime = $currentTime->format('Y-m-d H:i');
Solution 2 :
$currentDate = date('Y-m-d H:i:s');
$dateTime = new DateTime($currentDate);
$dateTime->modify('+330 minutes');
Solution 3 :
$currentDate = date('Y-m-d H:i:s');
$newTime = strtotime($currentDate . ' + 330 minute');
echo date('Y-m-d H:i:s', $newTime);
I think the above solution will be helpful for you though I haven't tested it.
When you use functions like date and strtotime, PHP converts the inputs you supply according to your date.timezone configuration in PHP, which may not be UTC. So it's important to check, or explicitly set, the timezone before you do the conversion.
$date = "30 Aug 2016 1:00 pm";
date_default_timezone_set("UTC");
var_dump(date_default_timezone_get()); // gives us "UTC"
var_dump(date("Y-m-d H:i:s", strtotime($date))); // 2016-08-30 13:00:00
var_dump(date("Y-m-d H:i:s", strtotime($date . "+5:30"))); // 2016-08-30 07:30:00
So now you're wondering why you just went back in time 5 hours and 30 minutes. Well, if you look at the actual Unix timestamp from strtotime the truth is revealed about what date and strtotime are doing.
var_dump(strtotime("30 Aug 2016 1:00 pm"), strtotime("30 Aug 2016 1:00 pm +5:30"));
This gives you...
int(1472562000)
int(1472542200)
In the first case, strtotime takes the input string "30 Aug 2016 1:00 pm" and converts it to a Unix timestamp under the assumption that we're currently in UTC. In the second case, it takes the input string "30 Aug 2016 1:00 pm +5:30", which already has a GMT offset of +5:30. So it assumes that we're 1:00 PM in GMT+0530, and it tries to convert that back to UTC (i.e +0000), meaning it now needs to subtract 5 hours and 30 minutes to get to UTC, which gives you "30 Aug 2016 7:30 am"
It's easier to avoid all this confusion when you use DateTime, because you can explicitly specify the timezone or the GMT offset and not be subject to implicit timezone conversion. Also, it's important to note that in PHP a timezone is more than just a GMT offset. PHP uses the Olson Timezone Database to reliably and accurately convert date/time information across different timezones, since GMT offsets can actually vary throughout the year in different timezones.
// DateTime doesn't try to convert this because you told it what the timezone is
$dt = new DateTime("30 Aug 2016 1:00 pm", new DateTimezone("UTC"));
// now lets try with the offset
$dt->setTimezone(new DateTimezone("Asia/Kolkata"));
var_dump($dt->format("j M Y g:i a")); // "30 Aug 2016 6:30 pm"
Notice, there's no need for you to actually mess around with how many hours/minutes to add/subtract. PHP figures it all out by looking up the needed timezone information in the database. Because we correctly specified the timezone supplied and the timezone converted we can rest assured we always have the accurate time regardless of how many times we subsequently convert between timezones. It's a far more reliable abstraction than what you're trying to do.
I got time stamp in this format from twitter api.
Fri Dec 28 20:06:38 +0000 2012
I want to convert this to standard time stamp format like this one.
2012-12-10 16:20:18
Am pretty new to dates in php. How can I do it??
You can use DateTime:
$date = new DateTime('Fri Dec 28 20:06:38 +0000 2012');
echo $date->format('Y-m-d H:i:s');
The reason why I prefer DateTime is that it gives great oop implementation and makes it easier to work with dates that are quite big head-ache of programmer. For more information about this class read the manual that I have already referenced.
You can use strtotime to convert the initial string to a UNIX timestamp and then strftime to convert it back to a string:
strftime('%Y-%m-%d %H:%M:%S', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
That call returns the string 2012-12-28 21:06:38 - i.e. exactly what you are looking for.
use this
$originalDate = "Fri Dec 28 20:06:38 +0000 2012";
echo $newDate = date("Y-m-d h:i:s", strtotime($originalDate));
working example http://codepad.viper-7.com/AhLcEU
In case of use php < 5.2.0 (some shared hostings) use a combination of strtotime and date
date('Y-m-d H:i:s', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
So I have the following
A match with a time and a timezone, this can be different from every match
A user with a timezone
Now what do I want?
I want to display the time for the user in his timezone so if the match is on 12:00 in GMT+0 (Europe/London) then I want it to be displayed as 13:00 for Europe/Amsterdam.
I have no idea how to do this, please help me out ;)
EDIT
Sorry, I use PHP
You can use PHP's DateTime class, which does a wonderful job of handling date/times and timezones. Here's an example of what you want.
/* Create a DateTime object using Europe/London Timezone */
$date = new DateTime("2012-12-10 12:00", new DateTimeZone("Europe/London"));
echo $date->format('r'); // Mon, 10 Dec 2012 12:00:00 +0000
/* Change the timezone to Europe/Amsterdam and output the new formatted date/time */
$date->setTimezone(new DateTimeZone("Europe/Amsterdam"));
echo $date->format('r'); // Mon, 10 Dec 2012 13:00:00 +0100
Need help in date conversion:
Existing date format:
Wed, 01 Dec 2010 00:00:00 -0500
(Day, DD MMM YYYY HH:MM:SS GMT-0500)
To be changed to:
2010-11-29T04:59:59-05:00
(YYYY-MM-DD(T)HH:MM:SS GMT-05:00)
How to handle in PHP?
is there any function available in php for this.
please help
strtotime() (man page) & date() (man page) or DateTime class (man page) should be able to handle this.
echo date('Y-m-d\TH:i:sZ', strtotime('Wed, 01 Dec 2010 00:00:00 -0500'));
echo date('c', strtotime('Wed, 01 Dec 2010 00:00:00 -0500')); // as mentioned by Anthony
or
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('Y-m-d\TH:i:sZ');
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('c'); // as mentioned by Anthony
First you want the date string in epoch format, so that you can use the date function. My favorite method to do this is the strtotime function:
$epoch_date = strtotime($original_date_string);
Now, you can use the date function to output it however you like. In your case, I believe you are looking for ISO 8601, which is built into the function:
$new_date_string = date('c', $epoch_date);
echo $new_date_string;
date('Y\-m\-d\Th:i:s \G\M\TP');
This will return:
2010-11-26T02:49:24 GMT-05:00
Use the date() formating its much simpler!
You can read all about it right here http://php.net/manual/en/function.date.php
I am parsing a xml, and I get the created time as: Wed, 24 Nov 2010 13:10:00 EST
My requirement is to convert this time into the specified time format using DateTime function before inserting it in the database.
Can anyone help me with this.
Thanks you
Zeeshan
set EST timezone
date_default_timezone_set('America/New_York');
convert to epoch
$epoch = strtotime('Wed, 24 Nov 2010 13:10:00 EST');
get date/time
echo date('Y-m-d H:i:s', $epoch);