We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    I want to list recent articles on a home page, but would like to have the dates displayed as follows:

    Recent Articles
    |- Article 5 (posted Today)
    |- Article 4 (posted Yesterday)
    |- Article 3 (posted Monday)
    |- Article 2 (posted Friday)
    |- Article 1 (posted 2 wks ago)


    So, it will need to compare the current system date/time to the article date source (according to &dateSource=``), and output:
    - for articles posted today or yesterday - ’Today’ or ’Yesterday’
    - for articles posted within the 7 days, excluding today and yesterday - the weekday it was posted on, eg ’Monday’ or ’Friday’
    - for articles older than one week, the number of weeks/months that have passed - eg ’2 wks ago’ or ’3 months ago’


    Would it be best to create a snippet or a PHX Custom modifier for this? For my purpose it would be used to formatting dates appearing in a Ditto list. Perhaps the PHX [+date:timesince+] modifier could be a good place to begin?


    [+date:timesince+] (added by Apox)

    <?php
    if (!function_exists('timeSince'))
    {
    	function timeSince($timestamp)
    	{
    		if ($timestamp >= time())
    		{
    			$timeago = '0 seconds ago.';
    			return $timeago;
    		}
    		$seconds = time()-$timestamp;
    		$units = array(
    			'year' => 31556926,
    			'month' => 2629743,
    			'day' => 86400,
    			'hour' => 3600,
    			'minute' => 60,
    //			'second' => 1
    		);
    		$timeago = '';
    		foreach ($units as $key => $val)
    		{
    			if ($seconds >= $val)
    			{
    				$results = floor($seconds/$val);
    				$seconds = ($seconds-($results*$val));
    				$timeago .= ($results >= 2) ? $results . ' ' . $key .  's, ' : $results . ' ' . $key .  ', ';
    			}
    		}	
    		return rtrim($timeago, ', ') . ' ago';
    	}
    }
    return timeSince($output);
    ?>
    
      • 1778
      • 659 Posts
      Hello

      Maybe this can help you, it’s extract from the quip.class.php (by Splittingred)...

      /**
           * Gets a proper array of time since a timestamp
           */
          public function timesince($input) {
              $output = '';
              $uts['start'] = strtotime($input);
              $uts['end'] = time();
              if( $uts['start']!==-1 && $uts['end']!==-1 ) {
                  if( $uts['end'] >= $uts['start'] ) {
                      $diff = $uts['end'] - $uts['start'];
                      $days = intval((floor($diff/86400)));
                      if ($days) $diff = $diff % 86400;
                      $hours = intval((floor($diff/3600)));
                      if ($hours) $diff = $diff % 3600;
                      $minutes = intval((floor($diff/60)));
                      if ($minutes) $diff = $diff % 60;
      
                      $diff = intval($diff);
                      $output = array(
                          'days' => $days
                          ,'hours' => $hours
                          ,'minutes' => $minutes
                          ,'seconds' => $diff
                      );
                  }
              }
              return $output;
          }
      

      Cheers
        • 33968
        • 863 Posts
        Many thanks for that - it looks like a good starting point.

        I’m not great with php but when I have a little more time to have a look at this properly I will post the results here. It might be useful for someone else too...