0

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

  • Welcome to SO. I'd recommend taking [the tour](https://stackoverflow.com/tour) and then reading [How to Ask](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) as an introduction to how SO works, and how to effectively ask a question. – Markus Deibel Aug 27 '19 at 04:18
  • Possible duplicate of [Timezone conversion in php](https://stackoverflow.com/questions/2505681/timezone-conversion-in-php) – Mike Doe Aug 27 '19 at 06:19

2 Answers2

1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Maros
  • 144
  • 1
  • 6
0

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?