Converting Date and Time from a specific Time Zone to another using PHP Tutorial

In this tutorial, you can learn how to Convert Date and Time from a specific Time Zone to another using PHP Language. The tutorial aims to provide students and beginners with a reference for learning to set and manage date time zones using PHP Language built-in class or object. Here, I will be providing a simple web page script that demonstrates the conversion of a date and time from a specific time zone into a different time zone.

What is Time Zone?

A Time Zone offsets from Universal Time Coordinate (UTC). A time zone is a region that adheres to a common standard time for social, commercial, and legal activities. Because it is easy for locations in frequent communication to preserve the same time, time zones typically follow the boundaries between countries and their subdivisions rather than rigidly following longitude.

How to convert Date and Time Zone from a specific Time Zone to another?

The Date and Time time zone can be easily converted from a specific time zone to another time zone using the PHP's built-in classes or objects called DateTime and DateTimeZone. These 2 PHP classes can be used to set the specific date and time into a certain Time Zone and generate the different time zone offset values.

Snippet

The below PHP snippet demonstrates the usage of the DateTime and DateTimeZone classes of PHP to convert the date time zones.

  1. <?php
  2. // March 25, 2023 12:45 AM
  3. $dateTime = "2023-03-25 00:45";
  4. // Set Date TimeZone as UTC
  5. $dateTime = new DateTime($dateTime, new DateTimeZone('UTC'));
  6. // Convert Date Time to 'Asia/Manila' Time Zone
  7. $dateTime->setTimezone(new DateTimeZone('Asia/Manila'));
  8. // Converted Value
  9. $convertedValue = $dateTime->format("F d, Y g:i A");
  10. // output: March 25, 2023 08:45 AM
  11. ?>

Example Web Page

The below scripts result in a simple web page written in HTML, CSS, and PHP. The contains a simple page layout with date and time form to convert the time zone from UTC to 'Asia/Manila'. The Formatted UTC and converted (Asia/Manila) date and time values are both shown in a single panel.

Page Interface

The following script is a PHP file script named index.php. It contains the HTML elements of the page layout, panels/containers, and form elements of the web page. This file also contains the PHP script for converting the entered date and time from UTC to Asia/Manila Time zone.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>PHP - Set Date Time Zone</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11. <link rel="stylesheet" href="style.css">
  12. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  13. </head>
  14. <div class="content-md-lg py-3">
  15. <div class="col-lg-6 col-md-10 col-sm-12 col-12 mx-auto">
  16. <div class="page-title">Converting Date and Time from specific Time Zone to another using PHP</div>
  17. </div>
  18. <hr style="margin:auto; width:25px" class="border-light opacity-100">
  19. <div class="container-lg">
  20. <div class="row py-3 justify-content-evenly">
  21. <div class="col-lg-4 col-md-5 col-sm-10 col-12">
  22. <div class="card bg-dark rounded-0 border-dark-subtle text-light">
  23. <div class="card-body rounded-0">
  24. <h3 class="text-center"><b>Select UTC Date and Time</b></h3>
  25. <hr class="border-light opacity-100 mx-auto" style="width:25px;height:2px">
  26. <form action="">
  27. <div class="mb-3">
  28. <label for="dateTime" class="text-white-50">Select UTC Date and Time</label>
  29. <input type="datetime-local" name="dateTime" value="<?= date("Y-m-d\Th:i", strtotime($_GET['dateTime'])) ?? '' ?>" class="form-control rounded-0" required="required">
  30. </div>
  31. <div class="text-center">
  32. <button class="btn btn-sm btn-primary rounded-0">Convert</button>
  33. </div>
  34. </form>
  35. </div>
  36. </div>
  37. </div>
  38. <div class="col-lg-4 col-md-5 col-sm-10 col-12">
  39. <div class="card bg-dark rounded-0 border-dark-subtle text-light">
  40. <div class="card-body rounded-0">
  41. <h3 class="text-center"><b>UTC to 'Asia/Manila' Time Zone</b></h3>
  42. <hr class="border-light opacity-100 mx-auto" style="width:25px;height:2px">
  43. <br>
  44. <br>
  45. <?php
  46. $dateTime = $_GET['dateTime'] ?? "";
  47. if(!empty($dateTime)){
  48. $dateTime = new DateTime($dateTime, new DateTimeZone('UTC'));
  49. //Original
  50. $originalValue = $dateTime->format("F d, Y g:i A");
  51. // Conver Date Time to diefferent Time Zone
  52. $dateTime->setTimezone(new DateTimeZone('Asia/Manila'));
  53. // Converted Value
  54. $convertedValue = $dateTime->format("F d, Y g:i A");
  55. }
  56. ?>
  57. <div class="mb-3">
  58. <dl>
  59. <dt class="text-center text-white-50">UTC Date and Time</dt>
  60. <dd class="text-center fw-bolder h3"><b><?= $originalValue ?? "----- --, ---- --:-- -- " ?></b></dd>
  61. </dl>
  62. </div>
  63. <div class="mb-3">
  64. <dl>
  65. <dt class="text-center text-white-50">Asia/Manila Date and Time</dt>
  66. <dd class="text-center fw-bolder h3"><b><?= $convertedValue ?? "----- --, ---- --:-- -- " ?></b></dd>
  67. </dl>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. </body>
  76. </html>

Stylesheet

The scipt below is the CSS file script known as style.css. It contains the custom style or codes for some of the page elements.

  1. @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Space+Mono&display=swap" rel="stylesheet');
  2. :root{
  3. --space-mono-font: 'Space Mono', monospace;
  4. --border-dark-subtle: #373838;
  5. }
  6. *{
  7. box-sizing: border-box;
  8. }
  9. body *{
  10. font-family: var(--space-mono-font);
  11. }
  12. /**
  13. Page Design
  14. */
  15. body,
  16. html{
  17. height: 100%;
  18. width: 100%;
  19. margin: 0;
  20. padding: 0;
  21. }
  22. body{
  23. background-color: #282A3A;
  24. }
  25. .page-title{
  26. font-size: 2.5rem;
  27. font-weight: 500;
  28. color: #fff;
  29. letter-spacing: 3px;
  30. font-family: var(--secular-font);
  31. text-align: center;
  32. text-shadow: 0px 0px 3px #2020208c;
  33. }
  34. .border-dark-subtle{
  35. border-color: var(--border-dark-subtle) !important;
  36. }
  37.  

Snapshots

The images below are the snapshots of the overall result of the web page scripts that I provided above.

Form Panel

Converting Date Time Time Zone using PHP

Convertion Panel

Converting Date Time Time Zone using PHP

Page Layout

Converting Date Time Time Zone using PHP

There you go! I have also provided the complete source code zip file of the web page scripts that I provided above on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify it to do some experiments to enhance your programming capabilities.

That's it! I hope this Converting a Date and Time from a specific Time Zone to another using PHP Tutorial will help you with what you are looking for and will be useful for your current and future PHP Projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment