Jump to content

Archived

This topic is now archived and is closed to further replies.

MadDawgX

Date Format

Recommended Posts

Hey there,

Im getting the date from a MySQL database like follow:

[code]DATE_FORMAT(date, '%Y-%m-%d %r') as comdate[/code]

That returns the time in the MySQL datase which is the Greenwhich time. Later on down the line I display this time.

My question is, Is it possible to display the time so that it is the same as the user's timezone. So for example EST would -5 from the hour.

- Thanx

Share this post


Link to post
Share on other sites
if user time is $x hours different from server time, try

echo date ('Y-m-d H:i:s', strtotime("+$x hours $dbtime"));

Share this post


Link to post
Share on other sites
@barand

I tired that and I got some unintelligible string of numbers. Maybe I did something wrong.

Anyways, I just wrote a function that will add time or subtract time based on the time zone.  Now in this function the user_zone is the difference between their time and the servers local time. I hope that I did not waste my time writting this.

The only problem that I see with this function is if it is the end or beging of the month the month does not change. I will continue working on this. If anyone has a fix for this and the potential leap year issue, please post it.

[code]<?php
function format_time() {
    $system_date = "2006-08-07 09:54:30 PM";
    $user_zone = "+ 4";

    list($action, $subject) = explode(" ", $user_zone);
    list($date, $time, $ext) = explode(" ", $system_date);
    list($hr, $min, $sec) = explode(":", $time);
    list($year, $month, $d) = explode("-", $date);
   
    if (($action == "-") * ($ext == "AM")) {
        $a = floor($hr - $subject);
        if ($a < 0) {
           $b = floor($hr + 12);
           $hour = floor($b - $subject);
           $extention = "PM";
           $day = floor($d - 1);
       }elseif ($hr == 12) {
           $hour = $a;
           $extention = "PM";
           $day = floor($d - 1);
       }else{
          $hour = $a;
          $extention = "AM";
       }
    }elseif (($action == "+") * ($ext == "AM")) {
        $a = floor($hr + $subject);
        if ($a > 12) {
            $hour = floor($a - 12);
            $extention = "PM";
            $day = floor($d + 1);
        }else{
            $hour = $a;
            $extention = "AM";
        }
    }elseif (($action == "-") * ($ext == "PM")) {
       $a = floor($hr - $subject);
       if ($a < 0) {
           $b = floor($hr + 12);
           $hour = floor($b - $subject);
           $extention = "AM";
           $day = floor($d - 1);
      }else{
          $hour = $a;
          $extention = "PM";
      }
   }elseif (($action == "+") * ($ext == "PM")) {
      $a = floor($hr + $subject);
      if ($a > 12) {
         $hour = floor($a - 12);
         $extention = "AM";
         $day = floor($d + 1);
      }else{
         $hour = $a;
         $extention = "PM";
      }
   }else{
      $hour = $hr;
      $extention = $ext;
   }
   $result = '' . $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $min . ':' . $sec . ' ' . $extention . '';
   return $result;
}

$time = format_time();
echo "$time";  
?>[/code]

Good Luck,
Tom

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.