skip to Main Content

I am new to this forum and learning to code.

I want to ask how can I change the time stamp to current timezone or is there any way I can convert the timestamp date/time to my current time zone America/Toronto or -4:00

I tried every single option available for MySQL and Cpanel. (htacces, phpini, settimezone in mysql)

Now I am planning to do with PHP as it will be better.

This is the code which is fetching the timestamp from table

<?php echo htmlentities($result->PostingDate);?>

Please tell me a suitable way to convert the time to current timezone

2

Answers


  1. You can do something like this

    mysql> SELECT CURRENT_TIMESTAMP;
    +---------------------+
    | CURRENT_TIMESTAMP   |
    +---------------------+
    | 2008-07-19 03:08:37 |
    +---------------------+
    1 row in set (0.00 sec)
    
    mysql> SET TIME_ZONE = '-00:00';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SELECT CURRENT_TIMESTAMP;
    +---------------------+
    | CURRENT_TIMESTAMP   |
    +---------------------+
    | 2008-07-19 07:10:13 |
    +---------------------+
    1 row in set (0.00 sec)
    

    which in this case the host uses the time zone MST (GMT-7). But Why not use UTC() time?

    Login or Signup to reply.
  2. If you know your current timezone that your PostingDate is set to, and you want to do it with PHP code, you can try this:

    // Current timezone used by server, database
    $current_timezone = new DateTimeZone('UTC'); 
    // Your timezone that YOU want to use
    $local_timezone = new DateTimeZone('America/Toronto');
    
    $datetime = new DateTime($result->PostingDate, $current_timezone);
    $datetime->setTimezone($local_timezone);
    
    // $datetime should be set to America/Toronto now.
    

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search