Jump to content

Time and Date Question?


Solarpitch

Recommended Posts

Hi,

 

I currently have the date and time being stored in my database as this format: February 16, 2008; 12:06 pm .. how can I take this away from the current time and date so that it will say beside a user's comment:

 

Uploaded: 3 hours ago, or Uploaded: 6 days ago

 

same idea as the comments on Bebo.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/91784-time-and-date-question/
Share on other sites

You can try strtotime(): http://ca.php.net/manual/en/function.strtotime.php to get a timestamp then subtract that from time(). Divide the difference by 60 for minutes, another 60 for hours, another 24 for days, etc.

 

May be a simpler way but first thing that came to mind.

problem strtotime had a problem with the ';' in the string

so have to remove that before getting all the stats.

but it's pretty simple

<?php
  header('Content-type: text/plain');
  $da="February 16, 2008; 12:06 pm";
  $da=str_replace(';','',$da);
  $diff=time();
  $dad=strtotime($da);
  $diff-=$dad;
  $weeks=intval($diff/(7*24*60*60));
  $diff%=(7*24*60*60);
  $days=intval($diff/(24*60*60));
  $diff%=(24*60*60);
  $hrs=intval($diff/(60*60));
  $diff%=(60*60);
  $min=intval($diff/60);
  $secs=$diff%=60;
  $ago= (!empty($weeks)?"$weeks week".($weeks!=1?'s':'').',':'').
	((!empty($days) || !empty($weeks))?"$days day".($days!=1?'s':'').',':'').
	((!empty($hrs) || !empty($days) || !empty($weeks))?"$hrs hour".($hrs!=1?'s':'').',':'').
	((!empty($min) || !empty($hrs) || !empty($days) || !empty($weeks))?"$min minute".($min!=1?'s':'').',':'').
	"$secs second".($secs!=1?'s':'');
  echo $ago;

  
?>

yes i agree with schilly, i have taken the liberty to write a few functions... choose one, and see how it works

 

<?php

$date = "February 11th 2008; 12:16 am";


function rounddown_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$date)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = explode(".",$dTime / 604800);
               return($since[0]."Weeks");
            }
            $since = explode(".",$dTime / 86400);
            return($since[0]." Days");
         }
         $since = explode(".",$dTime / 3600);
         return($since[0]." Hours");
      }
      $since = explode(".",$dTime / 60);
      return($since[0]." Minutes");
   }
   return substr($dTime,0,2)." Seconds";
}

function roundup_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$date)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = $dTime / 604800;
               return number_format($since)." Weeks";
            }
            $since = $dTime / 86400;
            return number_format($since)." Days";
         }
         $since = $dTime / 3600;
         return number_format($since)." Hours";
      }
      $since = $dTime / 60;
      return number_format($since)." Minutes";
   }
   return number_format($dTime)." Seconds";
}

function Extended_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$date)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = explode(".",$dTime / 604800);
               $since[1] = 7 * ('0.'.$since[1]);
               $since[1] = explode(".",$since[1]);
               return($since[0]."Weeks & ".$since[1][0]." Days");
            }
            $since = explode(".",$dTime / 86400);
            $since[1] = 24 * ('0.'.$since[1]);
            $since[1] = explode(".",$since[1]);
            return($since[0]." Days & ".$since[1][0]." Hours");
         }
         $since = explode(".",$dTime / 3600);
         $since[1] = 60 * ('0.'.$since[1]);
         $since[1] = explode(".",$since[1]);
         return($since[0]." Hours & ".$since[1][0]." Minutes");
      }
      $since = explode(".",$dTime / 60);
      $since[1] = 60 * ('0.'.$since[1]);
      $since[1] = explode(".",$since[1]);
      return($since[0]." Minutes & ".$since[1][0]." Seconds");
   }
   return substr($dTime,0,2)." Seconds";
}

echo(roundup_timedifference($date_entry)."<BR>");
echo(rounddown_timedifference($date_entry)."<BR>");
echo(Extended_timedifference($date_entry)."<BR>");

?>

 

try this script on a local test machine, and change the time, code is pretty simple really.

 

the function argument can take either a unix timestamp or a formatted date/time string compatible with strtotime();

 

roundup_timedifference will round up any remainder from the dividing (eg 1.5 hours = 2 hours, 1.4 hours = 1 hours)

rounddown_timedifference will round down any remainder from the dividing (eg 1.9 hours = 1 hours)

 

Extended_timedifference will take the remainder and produce sub units (eg 1.5 hours = 1 hours and 30 minutes)

^^ EXCEPT on seconds, seconds will be rounded down on this function

----

each function will return seconds/minutes/days and weeks. easily modified to return years, centuries etc.

 

hope this helps,

oops error in those functions, use this instead:

 

<?php

$datef = "February 11th 2008; 12:16 am";


function rounddown_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$start_unix)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = explode(".",$dTime / 604800);
               return($since[0]."Weeks");
            }
            $since = explode(".",$dTime / 86400);
            return($since[0]." Days");
         }
         $since = explode(".",$dTime / 3600);
         return($since[0]." Hours");
      }
      $since = explode(".",$dTime / 60);
      return($since[0]." Minutes");
   }
   return substr($dTime,0,2)." Seconds";
}

function roundup_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$start_unix)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = $dTime / 604800;
               return number_format($since)." Weeks";
            }
            $since = $dTime / 86400;
            return number_format($since)." Days";
         }
         $since = $dTime / 3600;
         return number_format($since)." Hours";
      }
      $since = $dTime / 60;
      return number_format($since)." Minutes";
   }
   return number_format($dTime)." Seconds";
}

function Extended_timedifference($start_unix){
   if(!is_int($start_unix)){
      $start_unix = strtotime(str_replace(";","",$start_unix)) or die("Unknown Date Format/ strtotime() Failure");
   }
   $dTime = microtime(true)-$start_unix;
   if(($dTime / 60) > 1){ // if true is 1 min or more
      if(($dTime / 3600) >= 1){ // if true is 1 hour or more
         if(($dTime / 86400) >= 1){ // if true is 1 day or more
            if(($dTime / 604800) >= 1){ // if true is 1 week or more
               $since = explode(".",$dTime / 604800);
               $since[1] = 7 * ('0.'.$since[1]);
               $since[1] = explode(".",$since[1]);
               return($since[0]."Weeks & ".$since[1][0]." Days");
            }
            $since = explode(".",$dTime / 86400);
            $since[1] = 24 * ('0.'.$since[1]);
            $since[1] = explode(".",$since[1]);
            return($since[0]." Days & ".$since[1][0]." Hours");
         }
         $since = explode(".",$dTime / 3600);
         $since[1] = 60 * ('0.'.$since[1]);
         $since[1] = explode(".",$since[1]);
         return($since[0]." Hours & ".$since[1][0]." Minutes");
      }
      $since = explode(".",$dTime / 60);
      $since[1] = 60 * ('0.'.$since[1]);
      $since[1] = explode(".",$since[1]);
      return($since[0]." Minutes & ".$since[1][0]." Seconds");
   }
   return substr($dTime,0,2)." Seconds";
}

echo(roundup_timedifference($datef)."<BR>");
echo(rounddown_timedifference($datef)."<BR>");
echo(Extended_timedifference($datef)."<BR>");

?>

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.