• We just launched and are currently in beta. Join us as we build and grow the community.

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

W8RK

Neko Character Designer
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
76
Likes
124
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    PHP - Set Date Time Zone</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0"

    /

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    >
  12. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  13. <script

    src

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"

    ></

    script

    >
  14. </

    head

    >
  15. <body

    >
  16. <div

    class

    =

    "content-md-lg py-3"

    >
  17. <div

    class

    =

    "col-lg-6 col-md-10 col-sm-12 col-12 mx-auto"

    >
  18. <div

    class

    =

    "page-title"

    >

    Converting Date and Time from specific Time Zone to another using PHP</

    div

    >
  19. </

    div

    >
  20. <hr

    style

    =

    "margin:auto; width:25px"

    class

    =

    "border-light opacity-100"

    >
  21. <div

    class

    =

    "container-lg"

    >
  22. <div

    class

    =

    "row py-3 justify-content-evenly"

    >
  23. <div

    class

    =

    "col-lg-4 col-md-5 col-sm-10 col-12"

    >
  24. <div

    class

    =

    "card bg-dark rounded-0 border-dark-subtle text-light"

    >
  25. <div

    class

    =

    "card-body rounded-0"

    >
  26. <h3

    class

    =

    "text-center"

    ><b

    >

    Select UTC Date and Time</

    b

    ></

    h3

    >
  27. <hr

    class

    =

    "border-light opacity-100 mx-auto"

    style

    =

    "width:25px;height:2px"

    >
  28. <form

    action

    =

    ""

    >
  29. <div

    class

    =

    "mb-3"

    >
  30. <label

    for

    =

    "dateTime"

    class

    =

    "text-white-50"

    >

    Select UTC Date and Time</

    label

    >
  31. <input

    type

    =

    "datetime-local"

    name

    =

    "dateTime"

    value

    =

    "<?= date("

    Y-m-d\Th:i", strtotime($_GET['dateTime'])) ?? '' ?>

    " class="form-control rounded-0" required="required">
  32. </

    div

    >
  33. <div

    class

    =

    "text-center"

    >
  34. <button

    class

    =

    "btn btn-sm btn-primary rounded-0"

    >

    Convert</

    button

    >
  35. </

    div

    >
  36. </

    form

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >
  40. <div

    class

    =

    "col-lg-4 col-md-5 col-sm-10 col-12"

    >
  41. <div

    class

    =

    "card bg-dark rounded-0 border-dark-subtle text-light"

    >
  42. <div

    class

    =

    "card-body rounded-0"

    >
  43. <h3

    class

    =

    "text-center"

    ><b

    >

    UTC to 'Asia/Manila' Time Zone</

    b

    ></

    h3

    >
  44. <hr

    class

    =

    "border-light opacity-100 mx-auto"

    style

    =

    "width:25px;height:2px"

    >
  45. <br

    >
  46. <br

    >
  47. <?php
  48. $dateTime =

    $_GET[

    'dateTime'

    ]

    ?? ""

    ;
  49. if(

    !empty(

    $dateTime)

    )

    {
  50. $dateTime =

    new DateTime

    (

    $dateTime, new DateTimeZone(

    'UTC'

    )

    )

    ;
  51. //

    Original
  52. $originalValue =

    $dateTime->

    format("F d, Y g:i A");
  53. // Conver Date Time to diefferent Time Zone
  54. $dateTime->setTimezone(new DateTimeZone('Asia/Manila'));
  55. // Converted Value
  56. $convertedValue = $dateTime->format("F d, Y g:i A");
  57. }
  58. ?>
  59. <div

    class

    =

    "mb-3"

    >
  60. <dl

    >
  61. <dt

    class

    =

    "text-center text-white-50"

    >

    UTC Date and Time</

    dt

    >
  62. <dd

    class

    =

    "text-center fw-bolder h3"

    ><b

    ><?=

    $originalValue ?? "----- --, ---- --:-- -- "

    ?></

    b

    ></

    dd

    >
  63. </

    dl

    >
  64. </

    div

    >
  65. <div

    class

    =

    "mb-3"

    >
  66. <dl

    >
  67. <dt

    class

    =

    "text-center text-white-50"

    >

    Asia/Manila Date and Time</

    dt

    >
  68. <dd

    class

    =

    "text-center fw-bolder h3"

    ><b

    ><?=

    $convertedValue ?? "----- --, ---- --:-- -- "

    ?></

    b

    ></

    dd

    >
  69. </

    dl

    >
  70. </

    div

    >
  71. </

    div

    >
  72. </

    div

    >
  73. </

    div

    >
  74. </

    div

    >
  75. </

    div

    >
  76. </

    div

    >
  77. </

    body

    >
  78. </

    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. }


Snapshots

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

Form Panel

Convertion Panel

Page Layout

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top