1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get the item age ... date command...

Discussion in 'PHP' started by redhits, Oct 25, 2007.

  1. #1
    Hello I want to known how old some documents are...

    so until now i have

    - my current date , ex: 26-10-2007

    and the date when documents were created ex , 11-10-2007, or 1-12-2005,6-6-2006,

    Is there any way to quickly display the age of the documents?!

    as $age=$today-$creation_date ?!
     
    redhits, Oct 25, 2007 IP
  2. Fash

    Fash Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This should do the trick:
    time() - filectime($file)
    Code (markup):
    Filectime() only works on Windows, though. From what I understand, most filesystems don't save file creation date.
     
    Fash, Oct 25, 2007 IP
    redhits likes this.
  3. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #3
    Sorry , you didn't got my question ... my problem it's how to get the days, months and years...
    not the file creation date...

    I got all the items dates in a database...
    now..
    if i do something like
    <?php

    $today='4-2-2007';
    $yesterday='1-1-2006';

    $write=date($today-$yesterday);
    echo $write;
    ?>
    it will out 3 , so it's not calculating the days, motnhs,etc...


    And i want it to report back "1 year, 1 month and 3 days!"
     
    redhits, Oct 25, 2007 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4
    Any help , pls, especially from nick _swd
     
    redhits, Oct 26, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I wrote this function a while ago, which will help here. It compares two timestamps and returns the difference like you want it.
    
    function calc_date_diff($timestamp_past = false, $timestamp_future = false, $years = true, $months = true, $days = true, $hours = true, $mins = true, $secs = true)
    {
    	// Use current time if parameter is FALSE.
    	if (!$timestamp_past)   $timestamp_past = time();
    	if (!$timestamp_future) $timestamp_future = time();
    	
    	// Attempt to convert strings to timestamp if necessary.
    	$timestamp_future = is_string($timestamp_future) ? strtotime($timestamp_future) : intval($timestamp_future);
    	$timestamp_past   = is_string($timestamp_past)   ? strtotime($timestamp_past)   : intval($timestamp_past);
    	
    	$diff = $timestamp_future - $timestamp_past;
    	$calc_times = array();
    	$timeleft   = array();
    	
    	// Prepare array, depending on the output we want to get.
    	if ($years)  $calc_times[] = array('Year',   'Years',   31104000);
    	if ($months) $calc_times[] = array('Month',  'Months',  2592000);
    	if ($days)   $calc_times[] = array('Day',    'Days',    86400);
    	if ($hours)  $calc_times[] = array('Hour',   'Hours',   3600);
    	if ($mins)   $calc_times[] = array('Minute', 'Minutes', 60);
    	if ($secs)   $calc_times[] = array('Second', 'Seconds', 1);
    
    	foreach ($calc_times AS $timedata)
    	{
    		list($time_sing, $time_plur, $offset) = $timedata;
    		
    		if ($diff >= $offset)
    		{
    			$left = floor($diff / $offset);
    			$diff -= ($left * $offset);
    			$timeleft[] = "{$left} ". ($left == 1 ? $time_sing : $time_plur);
    		}
    	}
    	
    	return $timeleft ? ($timestamp_future > $timestamp_past ? null : '-') . implode(', ', $timeleft) : 0;
    }
    
    
    PHP:
    Usage example:
    
    $age = calc_date_diff(filectime('file.zip'));
    // Returns (Eg): 7 Months, 6 Days, 11 Hours, 56 Minutes, 29 Seconds
    echo $age;
    
    PHP:
     
    nico_swd, Oct 26, 2007 IP