Friday 20 March 2020

Easiest Way To Change Date From One Timezone To Any Another Timezone | PHP And MYSQL | CONVERT_TZ


Code :-
<style type="text/css">
    body {
  font-family: Arial, Helvetica, sans-serif;
}
</style>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'test';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

if(! $conn ) {
   die('Could not connect: ' . mysqli_error());
}
else
{
    echo "DATABASE CONNECTED<br/>";
}

//CONVERTING TIME FROM NEW YORK(USA) -> KOLKATA(INDIA)
//FROM = NEW YORK
// TO = KOLKATA
$from_exact_date_time='2020-02-15 09:00:00';
$from_timezone=new DateTimeZone('America/New_York');
$from_datetime=new DateTime('now',$from_timezone);
$from_format=$from_datetime->format('P');
echo "New York format : ".$from_format."<br/>";

$to_timezone=new DateTimeZone('Asia/Kolkata');
$to_datetime=new DateTime('now',$to_timezone);
$to_format=$to_datetime->format('P');
echo "Kolkata format : ".$to_format."<br/>";

$query="SELECT CONVERT_TZ('".$from_exact_date_time."','".$from_format."', '".$to_format."') as converted_date";
$data=mysqli_query($conn,$query);

$output=mysqli_fetch_array($data);

echo "NEW YORK DATE/TIME IS : ".$from_exact_date_time."<br/>";
echo "KOLKATA DATE/TIME IS  : ".$output['converted_date'];


?>
</body>
------------------------------------------------
Please avoid or flag spams/hateful comments. And do not spam. Enjoy :)
------------------------------------------------
World Time Buddy :-
https://www.worldtimebuddy.com/

List of timezone : -
https://www.php.net/manual/en/timezones.php

Code Link : -

Subscribe my Channel:-
http://www.youtube.com/user/SanketRooney?sub_confirmation=1

Facebook Page:-
https://www.facebook.com/UnpossibleNS

Twitter Account:-
https://twitter.com/UnpossiblePOG

Blog :-
https://unpossiblepog.blogspot.in/
------------------------------------------------

Hey guys, this is UNPOSSIBLE POG.
In this video, I shall tell you the easiest way to convert date-time from one timezone to any other timezone using PHP and mysql.
In this example I will be converting NEW YORK time to KOLKATA time.
There is a website called “worldtimebuddy” (NOT SPONSORED) which will tell you exact time difference between two timezones.
Link in the description.
I am adding kolkata time and if you click on it, you will see that when in NEW YORK, the date-time is 15th of march 2020 2:00AM, at that time in kolkata, the time is 15th of marth 2020 11:30 AM.
Moving back to code I already added a code of database connection of MYSQLI.
In our code, FROM is NEW YORK and TO is KOLKATA.
First we shall get timezone.
In PHP manual, you will get all the available timezone list. Search for desired timezone.
Then in code, we shall get datetime of that timezone.
It’s link is also given in description.
Then get the difference format.

Same thing we have to do for “TO Timezone” that is, for KOLKATA.
Lets verify.
Remember that those formats are based on UTC.
If you want to convert from UTC then the format is always +00:00.
Then we write an SQL query, there is a function in query called CONVERT_TZ with 3 arguments.
1st argument is the time that you want to convert.
2nd argument is the format of that date, in your case, it is NEW YORK’s format.
3rd argument is the format of timezone that we want to convert.
If everything is correct, then the should be 15 march 2020 : 11:30 AM.
Lets try another date-time.
If in new york the time is 9 AM, then in kolkata it should be 18:30 of same date.
Required links are given in the description.
Thanks for watching. 
Don’t forget to like, share and subscribe.

No comments:

Post a Comment