Jump to content


Photo

Date Format


  • Please log in to reply
2 replies to this topic

#1 MadDawgX

MadDawgX
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 07 August 2006 - 09:57 PM

Hey there,

Im getting the date from a MySQL database like follow:

DATE_FORMAT(date, '%Y-%m-%d %r') as comdate

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



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 August 2006 - 11:51 PM

if user time is $x hours different from server time, try

echo date ('Y-m-d H:i:s', strtotime("+$x hours $dbtime"));
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 August 2006 - 03:11 AM

@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.

<?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";	  		 											
?>

Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users