Jump to content

Function Snippets


GuruMeditation
 Share

Recommended Posts

Hello everyone. 
 
I thought I'd share a few of the functions I've been working on in case any new users etc find them useful. I've not been programming in PHP long, so excuse the sloppy code, and if you find any errors etc, let me know and I will update it.
 
The following function basically takes your $page date and returns it in either hours and minutes if the page is less than a day old, or as sandard if it's older. This could also be extended to display the date as yesterday etc as well.

If people are interested, I can add more, and feel free to add your own.

function formatDate($itemDate)
{

  // Let's check the item date against the current date so that we can work out the date and time difference.
  $itemDateTime = new DateTime($itemDate);
  $currentDateTime = new DateTime('now');
  $interval = $itemDateTime->diff($currentDateTime);
  $day = $interval->format('%d');
  $hour = $interval->format('%h');
  $minute = $interval->format('%i');

  // If it's less than a day, display the date as hours and minutes since the post.
  // $day == 0 means there is no day difference, i.e we know it is on the same day.
  if($hour < 24 && $day == 0){
    // If it's been less than an hour.
    if($hour < 1){
      if($minute == 0){
        $itemDate = "less than a minute ago";
      }
      elseif($minute == 1){
        $itemDate = $minute . " minute ago";
      }
      else{
        $itemDate = $minute . " minutes ago";
      } 
    }
    // If it's been more than an hour and less than a day.
    elseif($hour >= 1 && $hour < 24){
      if($hour == 1 && $minute == 0){
        $itemDate = $hour . " hour ago";
      }
      elseif($hour == 1 && $minute == 1){
        $itemDate = $hour . " hour and " . $minute . " minute ago";
      }
      elseif($hour == 1 && $minute > 1){
        $itemDate = $hour . " hour and " . $minute . " minutes ago";
      }
      elseif($hour > 1 && $minute == 0){
        $itemDate = $hour . " hours ago";
      }
      elseif($hour > 1 && $minute == 1){
        $itemDate = $hour . " hours and " . $minute . " minute ago";
      }
      elseif($hour > 1 && $minute > 1){
        $itemDate = $hour . " hours and " . $minute . " minutes ago";
      }
    }
  }
  // If it's more than a day, just post the standard date.
  else {
    $itemDate = $itemDate;
  }

return $itemDate;
}

The following function can be used in conjunction with the formatDate($itemDate) above.
 

/* This function outputs each article / page header according to how we want it.
** $icon = Font Awesome icon etc, i.e "fa fa-link" which could be used if we create a links system.
** $item = Usually the $page we call the function from.
** $type = A string in the form of Article, Link etc.
** Example: If we call the function with the following args showItemHeader("fa fa-link", $link, "Link");
** We will see the following displayed: (link icon) Link published 22 hours ago in Website Links 
** Obviously change the markup to suit your own needs.
*/

function showItemHeader($icon, $item, $type)
{

 echo "<h3><a href='{$item->url}'>{$item->title}</a></h3>" .
      "<h6><i class='$icon'></i> " . $type . " published: " . formatDate($item->date) . " in " . "<a href='{$item->parent->url}'>{$item->parent->title}</a></h6>";

}
  • Like 1
Link to comment
Share on other sites

There's a function for that in core

echo wireRelativeTimeStr($page->modified);
You're welcome.

Just a note: Doesn't work well with when caching. ;)

I'm sorry to tell you.

In /wire/core/functions.php there you have the wireRelativeTimeStr function.

echo wireRelativeTimeStr(1400586357); // 1400586357 = unix timestamp

Sorry Gurumeditation...

Between the amazing stuff in ProcessWire and the stellar support community, it can be hard to do any work around here! Talk about "RelativeTime": 9 minutes from post to double answer.

Thanks,

Matthew

  • Like 3
Link to comment
Share on other sites

LOL - No need to apologise. I often reinvent the wheel. It's great to know these methods.

How do you find this magic? Where is it hidden? I'm keen to find the other hidden goodies.

Oh well, it was a nice coding exercise.  :lol:

Edit: I found the hidden goodies in the Functions.php file in wire/core (as Martijn mentioned, and I missed) - Nice.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...