You need to use the date() function to format it as you want. The date function takes a string for the format as the first parameter and an optional timestamp parameter. You will need to convert your string date from MySQL into a timestamp using the strtotime function.
Depends on what time is running on the MySQL server - if that’s GMT then it’s GMT… voila! Otherwise you might need to change it (look up MySQL functions for DATE_ADD and DATE_SUB)
To get the timezone:
function getTimeZone(){
if (function_exists(‘date_default_timezone_get’)) {
return date_default_timezone_get();
}
else {
return getenv(“TZ”);
}
}
@PHPycho that will only work when dealing with dates generated by the php server. If the dates are entered into the MySQL server using the MySQL server time (such as NOW() in a SQL statement) then the time will be in that server’s timezone. If you are using a different timezone in your PHP app, and treating the times out of MySQL as if they were from the same timezone then you will have bugs galore.
There should be single point of entry for data. So in my case i only use PHP’s date and pass to the mysql query. In this way you won’t face any problem. or am i missing somewhere???
That’s perfectly fine for you, as long as you keep to those rules - however your advise was based on rules that you hadn’t stated (such as only ever using the PHP server’s time).
It’s always safer to make sure mistakes can’t happen, and have everything running happily on the same timezone. If your php and db servers are at the same host it’s likely they’re set to the same timezone and you can just use PHP to figure out the current timezone, then alter all dates according to what timezone you want them to be in.
One simple Question:
Is there any effective method/techniques for dealing with timezone problems?
In my case, Since if you use only PHP’s there care should be taken (date should be fed from php to mysql). So i think this method may not be appropriate when you deal with NOW() or any other mysql date function in Query.
thanks guys. i have got it sorted, i think my time is being store in GMT (I guess… ) so I just include ‘GMT’ in the output haha - simple as that! thanks!
echo date('H:i:s') . ' GMT on ' . date('jS F Y', strtotime($sqldate));