Jump to content

Date Format


MadDawgX

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

Link to comment
https://forums.phpfreaks.com/topic/16836-date-format/
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
Link to comment
https://forums.phpfreaks.com/topic/16836-date-format/#findComment-70938
Share on other sites

Archived

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

×
×
  • Create New...

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.