We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17717
    • 433 Posts
    Wow, great bit of code, that would work a treat, would love to know how to set extra conditionals, the lazy option will do for now though smiley
      http://www.blend.uk.com | Web Design by Blend
      • 17717
      • 433 Posts
      Another cool feature would be something along the following:

      There are X new posts and X new comments since your last visit.

      Wonder how this might be integrated.

      Thanks,
      Lee
        http://www.blend.uk.com | Web Design by Blend
        • 14050
        • 788 Posts
        Hey Lee,

        If you were using the above code, make sure to notice a correct I just made. In the condition for the minutes, I forgot to make it ".=" The first version I posted just had "=" which would only show minutes and not days or hours.
          Jesse R.
          Consider trying something new and extraordinary.
          Illinois Wine

          Have you considered donating to MODx lately?
          Donate now. Every contribution helps.
          • 14050
          • 788 Posts
          Here is the updated code with the conditionals to handle the plurals that I was talking about:

          <?php
          $table = $modx->getFullTableName("site_content");
          $results = $modx->db->select("createdon", $table, "", "createdon DESC", "1");
          $results = $modx->db->getRow($results);
          $createdon = $results['createdon'];
          $now = time();
          $diff = $now - $createdon;
          
          $days = floor($diff/86400);
          $hours = floor($diff/3600);
          $minutes = floor($diff/60);
          
          
          
          if ($days > 0) 
          {
          	$output = $days . ' days ';
          	if ($days == 1) $output = $days . ' day ';
              $diff = $diff - ($days * 86400);
              $hours = floor($diff/3600);
          } 
          if ($hours > 0)
          {
          	$output .= $hours . ' hours ';
          	if ($hours == 1) $output .= $hours . ' hour ';
              $diff = $diff - ($hours * 3600);
              $minutes = floor($diff/60);
          }
          if ($minutes >0)
          {
          	$output .= $minutes . ' minutes';
          	if ($minutes == 1) $output .= $minutes . ' minute';
          }
          
          return $output;
          ?>
            Jesse R.
            Consider trying something new and extraordinary.
            Illinois Wine

            Have you considered donating to MODx lately?
            Donate now. Every contribution helps.
            • 14050
            • 788 Posts
            Silly error if there are 0 hours :-D

            <?php
            $table = $modx->getFullTableName("site_content");
            $results = $modx->db->select("createdon", $table, "", "createdon DESC", "1");
            $results = $modx->db->getRow($results);
            $createdon = $results['createdon'];
            $now = time();
            $diff = $now - $createdon;
            $days = floor($diff/86400);
            $hours = floor($diff/3600);
            $minutes = floor($diff/60);
            
            
            
            if ($days > 0) 
            {
            	$output = $days . ' days ';
            	if ($days == 1) $output = $days . ' day ';
                $diff = $diff - ($days * 86400);
                $hours = floor($diff/3600);
            
            } 
            if ($hours > 0)
            {
            	$output .= $hours . ' hours ';
            	if ($hours == 1) $output .= $hours . ' hour ';
                $diff = $diff - ($hours * 3600);
                $minutes = floor($diff/60);
            }
            else $minutes = floor($diff/60);
            if ($minutes >0)
            {
            	$output .= $minutes . ' minutes';
            	if ($minutes == 1) $output .= $minutes . ' minute';
            }
            
            return $output;
            ?>
              Jesse R.
              Consider trying something new and extraordinary.
              Illinois Wine

              Have you considered donating to MODx lately?
              Donate now. Every contribution helps.
              • 17717
              • 433 Posts
              Nice work my friend.

              Lee
                http://www.blend.uk.com | Web Design by Blend
                • 33337
                • 3,975 Posts
                Jesse, you planning to wrap it up and release as full snippet?

                that would be cool for sure smiley.

                regards.
                  Zaigham R - MODX Professional | Skype | Email | Twitter

                  Digging the interwebs for #MODX gems and bringing it to you. modx.link
                  • 14050
                  • 788 Posts
                  Sure, I didn’t think there would be any real demand for this. If I am going to wrap it up and put it in the repository. I guess I should take some feature requests, if any have them.
                    Jesse R.
                    Consider trying something new and extraordinary.
                    Illinois Wine

                    Have you considered donating to MODx lately?
                    Donate now. Every contribution helps.
                    • 11975
                    • 2,542 Posts
                    Hi,

                    perhaps you should check if the latest created document is published in your query

                    :-)

                    $results = $modx->db->select("createdon", $table, "published=1", "createdon DESC", "1");
                    


                    as a new feature you could add a parent id
                      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                      • 14050
                      • 788 Posts
                      <?php
                      /*
                      ::::::::::::::::::::::::::::::::::::::::
                       Snippet name: LastCreation
                       Short Desc: Lists the time from the last creation of a document
                       Version: 1.0.0
                       Author: Jesse Rochman (Jesster444)
                      	
                       Date: March 7, 2007
                       
                       Example Usage:
                       
                       [!LastCreation!] - Outputs the the time in xx Days xx Hours xx Minutes.  It will return the time of creation for the last document created, regardless of published status.
                       [!LastCreation? &lcPublished=`1` &lcParent=`10`!] - Outputs the time of creation for the last published document created that is a child of document 10.
                       
                       Parameters:
                       &lcPublished: (0|1)
                      If set to 0 it will output the time for the last creation of an unpublished document, if set to 1 it will output the time for the last creation of a published document.
                      
                      &lcParent:
                      The parent ID of the folder you want to look in for last creation times.  Can be used in conjuction with $lcPublished.
                       
                      ::::::::::::::::::::::::::::::::::::::::
                      */
                      
                      if (isset($lcPublished) && $lcPublished < 2) {
                      	$SQL = "published=$lcPublished";
                      	}
                      	else $SQL = '';
                      
                      if (isset($lcParent) && $SQL) {
                      	$SQL .= " AND parent=$lcParent";
                      	}
                      	elseif (isset($lcParent) && !$SQL) {
                      	$SQL = "parent=$lcParent";
                      	}
                      
                      
                      $table = $modx->getFullTableName("site_content");
                      
                      $results = $modx->db->select("createdon", $table, "$SQL", "createdon DESC", "1");
                      
                      $results = $modx->db->getRow($results);
                      
                      
                      $createdon = $results['createdon'];
                      
                      if ($createdon) {
                      	$now = time();
                      	$diff = $now - $createdon;
                      	$days = floor($diff/86400);
                      	$hours = floor($diff/3600);
                      	$minutes = floor($diff/60);
                      
                      
                      	if ($days > 0) {
                      		if ($days == 1) $output = $days . ' day ';    
                      		else $output = $days . ' days ';
                      
                      		$diff = $diff - ($days * 86400);
                      		$hours = floor($diff/3600);
                      
                      	} 
                      	if ($hours > 0) {
                      		if ($hours == 1) $output .= $hours . ' hour ';
                      		else $output .= $hours . ' hours ';
                      	
                      		$diff = $diff - ($hours * 3600);
                      		$minutes = floor($diff/60);
                      	}
                      	else $minutes = floor($diff/60);
                      	if ($minutes >0) {
                      		if ($minutes == 1) $output .= $minutes . ' minute';
                      		else $output .= $minutes . ' minutes';
                      	}
                      
                      	return $output;
                      }
                      else return '';
                      ?>


                      That should work then. If some people could test this for me. I tried to test everything I could. If there aren’t any bugs, then I will post this to the repository.
                        Jesse R.
                        Consider trying something new and extraordinary.
                        Illinois Wine

                        Have you considered donating to MODx lately?
                        Donate now. Every contribution helps.