For all the tables in my database, with all columns with date types, in PHP I want to be able to output the correct time in my timezone (GMT)
I'm also using a php function to convert the raw datetime from MySQL into a more readable format such as "1 day ago", "1 minute ago" etc..
The problem is in PhpMyAdmin, if I run the SQL Query "SELECT NOW()", I'm displayed with a time 4 hours behind my timezone, which I don't want.
For example, the time of writing is 17:03pm, but MySql returns it as 13:01pm.
So, whenever I test submit a form which posts data to the table, the record on the page shows submitted "4 hours ago" where instead it should say "x seconds ago", because the server is 4 hours behind.
The shared hosting server is configured to EST time and Namecheap hosting support said this can't be changed.
They provided me some PHP code which does return the correct time on the web page like this:
$today = date("H:i:s");
echo $today
My Answer:
To make the 'datetime' display 4 hours ahead of what PhpMyAdmin states, you can adjust the timezone settings in your PHP code. You can use the following code to set the timezone to GMT+4:
php
date_default_timezone_set('GMT+4');
You can add this line of code at the beginning of your PHP script or in a configuration file that is included in all your PHP scripts.
Additionally, when retrieving datetime values from your MySQL database, you can use the `CONVERT_TZ` function in your SQL queries to convert the datetime values to the desired timezone. For example:
sql
SELECT CONVERT_TZ(your_datetime_column, 'UTC', 'GMT+4') AS adjusted_datetime FROM your_table;
This will convert the datetime values from the 'UTC' timezone to the 'GMT+4' timezone before displaying them in your PHP application.
By setting the timezone in your PHP code and using the `CONVERT_TZ` function in your SQL queries, you can ensure that the datetime values are displayed correctly in your desired timezone.
Create blog | Privacy Policy | Terms & Conditions | Contact Us
Rate this post
4 of 5 based on 6481 votesComments