I have an old MySQL database. Here is a time column.
I see here is some time values Like:
2013-06-03 21:33:15
So, I want to convert this time to my local time UTC +6 in my PHP Script.
How can it possible?
I can make a mysql query to get the time from Database to my my PHP variable $TimeFromMySQL
Just now I want to show like:
11:32:44 PM 05 July 2014
Thank You
See VMai's comment above if you want to do this in MySQL. For PHP:
$inDate = '2013-06-03 21:33:15';
$inDate_tz = 'America/Chicago';
$original_date = new DateTime($inDate, new DateTimeZone($inDate_tz) );
$original_date->setTimeZone(new DateTimeZone('Asia/Dhaka'));
$new_date = $original_date->format('H:i:s d F Y');
echo $new_date;
//outputs 08:33:15 04 June 2013
My answer here might be too late, still it might be helpful for some one who run to the same situation like me before I work around this solution.
To convert datetime to UTC that is, getting correct location Time and Date.
I came up with this:
// Get time Zone.
$whereTimeNow = date_default_timezone_get();
// Set the time zone.
date_default_timezone_set($whereTimeNow);
$dateTime = date('d-m-Y H:i');
$dateTimeConvert = new DateTime($dateTime, new DateTimeZone($whereTimeNow) );
$dateTimeConvert->setTimeZone(new DateTimeZone('UTC'));
$dateTimeNow = $dateTimeConvert->format('Y-m-d H:i:s');
echo $dateTimeNow;
Related
I have an issue with converting DateTime from Moscow timezone to New York timezone. Here is my test script:
$year = 2015;
$month = 3;
$tzMoscow = new DateTimeZone('Europe/Moscow');
$tzNewYork = new DateTimeZone('America/New_York');
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzMoscow);
echo $startDate->format('Y-m-d H:i:s')."\n"; // 2015-03-01 16:16:05
$startDate = DateTime::createFromFormat('Y-n-d', "$year-$month-01", $tzNewYork);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 09:16:05
$startDate->setTimezone($tzMoscow);
echo $startDate->format('Y-m-d H:i:s') . "\n"; // 2015-03-01 17:16:05
Third output is incorrect, time should be 16:16:05. Am I doing something wrong or is this a bug in php?
I think I've got it. The problem is that you're not specifying a time of day, so createFromFormat is using "the current system time":
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
Now what does it mean to use "the current system time" to create a time in the New York time zone, when that time zone has changed UTC offset between the specified date (March 1st) and the current date (March 14th)? It was UTC-5 on March 1st, and it's now UTC-4 due to daylight saving time.
I believe PHP is taking the current time of day in the specified time zone (9:16 at the time you ran that code) and then using that as the time of day on the specified date. So we end up with March 1st 2015, 09:16 New York time - or March 1st, 14:16 UTC, which is indeed March 1st 17:16 Moscow time.
That's not the same as the current time of day in Moscow at the time that you ran the code, which was 16:16.
Basically, you should try not to do this - or expect problems like this to happen. Think about what time of day you're really trying to represent, bearing in mind that within a particular time zone, offsets change over time. I can't really advise you on what your code should be, because we don't know what you're trying to achieve - but using the current time of day for a different date can definitely cause this problem.
I also believe this is an issue of DST, changing in New York time zone on 8th of March. I extended your date formats to Y-m-d H:i:s U I to include the unix timestamp and the DST value.
The output is now as following:
2015-03-01 19:07:17 1425222437 0
2015-03-01 11:07:17 1425226037 0
2015-03-01 20:07:17 1425226037 0
As you can see, the unix timestamp is already different between both created DateTime objects.
Now when I specify a concrete time as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzMoscow);
as well as
$startDate = DateTime::createFromFormat('Y-n-d H:i', "$year-$month-01 00:00", $tzNewYork);
the output changes to:
2015-03-01 00:00:00 1425153600 0
2015-03-01 00:00:00 1425186000 0
2015-03-01 09:00:00 1425186000 0
On the other hand, if I change $month to 4 (where New York uses DST), I get the following output:
2015-04-01 19:08:35 1427900915 0
2015-04-01 11:08:35 1427900915 1
2015-04-01 19:08:35 1427900915 0
What do these results mean?
The conversion between New York and Moscow time zone works correct in all cases, as you can judge from the unix timestamp being identical. Also, "2015-03-01 00:00" are obviously different timestamps for Moscow and New York, because they depend on the concrete time zone.
So I think your code is correct and there is no bug in php. However, the "current" times in New York and Moscow differ due to the DST switch between 1st of March and today (14th of March).
So while Jon's answer already explained the theory (I don't want all the credit, he was first), maybe someone will still find some concrete examples useful.
You can use this function, for change between timezones
function changeTimezone($time, $currentTimezone, $timezoneRequired, $FormtsTime = 'Y-m-d h:i:s')
{
$dayLightFlag = false;
$dayLgtSecCurrent = $dayLgtSecReq = 0;
$system_timezone = date_default_timezone_get();
$local_timezone = $currentTimezone;
date_default_timezone_set($local_timezone);
$local = date($FormtsTime);
date_default_timezone_set("GMT");
$gmt = date($FormtsTime);
$require_timezone = $timezoneRequired;
date_default_timezone_set($require_timezone);
$required = date($FormtsTime);
date_default_timezone_set($system_timezone);
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
if ($dayLightFlag) {
$final_diff = $dayLgtSecCurrent + $dayLgtSecReq;
$date->modify("$final_diff seconds");
}
$timestamp = $date->format($FormtsTime);
return $timestamp;
}
Seems I don't quite understand much the function strtotime. My case is I would like to compare the current time (now) with a specific time on specific timezone
For example the specific time is "this Monday at 14:00:00" at the timezone "America/New_York":
$specificTime = strtotime("monday this week 14:00:00 America/New_York");
My current code is:
$now = strtotime("now America/New_York");
if ($now > $specificTime) {
//do something
}
But I have figured it out that $now above is 6 hours ahead with current time. The number 6 I guess from offset -05:00 of America/New_York, plus with 1 hour daylight saving.
it should remove timezone out of $now, it will work correctly:
$now = strtotime("now");
if ($now > $specificTime) {
//do something
}
Could someone give the explain why strtotime("now America/New_York") is 6 hours ahead with strtotime("now), why they are not equivalent? really confused.
P.S: I am on GMT+07:00.
Simple debugging:
<?php
$now = strtotime("now America/New_York");
echo date('r', $now);
// Thu, 28 Nov 2013 16:39:51 +0100
... shows that such command is doing this:
Calculate local time in my default time zone (10:39:51 +0100)
Return timestamp that corresponds to 10:39:51 in New York time (-0500)
Doing date manipulation with strings is terribly complicated. Just imagine you'd try to do math with string functions: strtofloat('one plus square root of half hundred')—there'd be plenty of room for mistakes. So my advise is to keep it simple and only use with simple expressions when there's some benefit, such as strtotime('+1 day').
If you need to work with different time zones, I suggest you use proper DateTime objects. If you choose to work with Unix timestamps, forget about time zones: Unix timestamps do not have time zone information at all.
You can use DateTime for this. I believe settings a timezone in strtotime is not valid.
$specificTime = new DateTime("monday this week 14:00:00", new DateTimeZone("America/New_York")));
$now = new DateTime("now", new DateTimeZone("America/New_York"));
You can then compare unix timestamp with this:
if ($now->getTimestamp() > $specificTime->getTimestamp()) {
// do something ...
}
There is time offset between each timezone.
strtotime() function will return the Unix timestamp according the timezone.
It will use the default time zone unless a time zone is specified in that parameter.
The default time zone it the return value of date_default_timezone_get();
Look the code below:
<?php
// UTC
echo date_default_timezone_get(), "\n";
// 2013-11-28 14:41:37
echo date('Y-m-d H:i:s', strtotime("now America/New_York")), "\n";
// 2013-11-28 09:41:37
echo date('Y-m-d H:i:s', strtotime("now")), "\n";
I am located in the (PDT) time zone at time Sat May 11 2013 20:58:51 (my time) I generated a date/time using the following code.
var date = new Date();
alert(date);
This returns the result
"Sat May 11 2013 20:58:51 GMT-0700 (PDT)"
If I then post this date to a php script which processes it in the following way:
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
I get the result
"2013-05-12 03:58:51"
Not what I expected. I only get the expected time if I get rid of the "GMT-0700 (PDT)" part from my date/time. So I have two questions.
Can anyone tell me how to generate a date of this format but with out the "GMT-0700 (PDT)" part in javascript without using string functions/regex?
Is my browser giving me the wrong time zone, or is php interpreting the time zone incorrectly. In either case, why?
JavaScript always take your local server timezone, while PHP is converting your date to UTC, so you need to set your server time zone before using strtotime as follow,
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
DEMO.
Or if you would like to convert your js date to UTC than use,
var date = new Date();
var utcdate = date.toUTCString()
$date = date('r', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
1- echo date("l F j, Y, H:i s");
2-
date_default_timezone_set('America/Los_Angeles');
$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);
There is a simple way to get unix time range of a day if given a random timestamp from that day ?
I have a date like 1345547471 which is "Tue, 21 Aug 2012 11:11:11 GMT"
There is a php function that can receive a timestamp like this and return a 00:00 hours timestamp and a 23:59 hours timestamp of that day ?
Thank you.
Sure, DateTime can do that:
$time = 1345547471;
$date = new DateTime;
// $date->setTimezone( new DateTimeZone( "America/New_York")); // Can set TZ here if needed
$date->setTimestamp( $time);
Now, you can set the time to whatever you want:
$date->setTime( 0, 0, 0); // 0h 0m 0s
And grab the resulting UNIX Timestamp:
$timestamp = $date->getTimestamp();
Same thing for the next use-case:
$date->setTime( 23, 59, 0);
$timestamp = $date->getTimestamp();
It is important to note that DateTime will properly handle cases of daylight savings time and local time discontinuities.
You can use the mod (gives the remainder after a division) PHP function like this to get the first second of a Unix timestamp (ie, today 0:00:00)
$var=time()-(time()%86400);
Then with this unix timstamp, you can add 86399 to get the last second of the day.
Edit: This doesn't account for dalylight savings.
$ts = 1345547471;
$ts_00_00 = mktime(0,0,0, date("m", $ts), date("d",$ts), date("Y",$ts);
$ts_23_59 = mktime(23,59,59, date("m", $ts), date("d",$ts), date("Y",$ts);
Documentation:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.mktime.php
If you are using PHP >= 5.3.0 Then you can use this...
Check out for this.
http://www.php.net/manual/en/datetime.createfromformat.php
This is similar to Fluffeh's answer, but accounts for daylight savings time. This is based on the server's time zone.
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00");
$end = strtotime(date("Y-m-d")." 23:59:59");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
If you want to specify a custom timezone instead of the server timezine, you can add it to like so:
//Get time range for today
$start = strtotime(date("Y-m-d")." 00:00:00 -0500");
$end = strtotime(date("Y-m-d")." 23:59:59 -0500");
//Show our date in a human-readable format for troubleshooting
echo date(DATE_RFC1036,$start)."<br>".date(DATE_RFC1036,$end);
Link to working Sample
I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.
The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30