Skip to content
Menu
Tutorial Wala
  • Interview
    • Html
    • Css
    • jQuery
    • Laravel
    • Flutter
  • Laravel
  • Flutter
  • CMS
  • Social Media
    • Instagram
    • Youtube
  • How To Become A ?
    • Ai Developer
    • App Developer
    • Asstronaut
    • Youtuber
  • Write For Us
Tutorial Wala
PHP converting DATETIME - Tutorial Wala
November 7, 2023November 7, 2023

PHP converting DATETIME into time ago function, seconds, minutes, hours, days etc

You can create a function in PHP to convert a DATETIME into a “time ago” format that displays the elapsed time in seconds, minutes, hours, days, etc. To do this, you can calculate the time difference between the given DATETIME and the current time, and then format the result accordingly. Here’s an example of such a function:

function timeAgo($dateTime) {
    $currentTimestamp = time();
    $dateTimeTimestamp = strtotime($dateTime);

    $timeDifference = $currentTimestamp - $dateTimeTimestamp;

    if ($timeDifference < 60) {
        return $timeDifference . ' seconds ago';
    } elseif ($timeDifference < 3600) {
        $minutes = floor($timeDifference / 60);
        return $minutes . ' minutes ago';
    } elseif ($timeDifference < 86400) {
        $hours = floor($timeDifference / 3600);
        return $hours . ' hours ago';
    } else {
        $days = floor($timeDifference / 86400);
        return $days . ' days ago';
    }
}

// Example usage:
$dateTime = '2023-11-01 12:00:00'; // Your DATETIME
$timeAgo = timeAgo($dateTime);
echo $timeAgo;

In this function:

  1. We calculate the time difference in seconds between the given DATETIME and the current time using the time() function and strtotime() function.
  2. Depending on the value of the time difference, we format the result to display the time in seconds, minutes, hours, or days.
  3. We return the formatted "time ago" string.

You can modify the function and the output format to suit your needs. This is a basic example, and you can add more conditions or customize it further as necessary.

Share on Social Media
x facebook pinterest linkedin emailwhatsapp

Related Posts

How To Install Flutter?
How to get a loan from the standard chartered bank in India
How to become an Astronaut ?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • React.js interview questions and answers for beginners.
  • Here are the steps to find the correct university and course before creating a Statement of Purpose (SOP)
  • Creating a React Vite Project Using npm
  • How To Import The Table Collection Json In Mongodb
  • How to Conditionally Hide Headers on the First and Last Page with mPDF

Recent Comments

No comments to show.

Archives

  • September 2024
  • June 2024
  • March 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023

Categories

  • Actor
  • AI Developer ?
  • App Developer
  • Astronaut
  • Blog
  • Css
  • Css
  • Database
  • Education
  • Flutter
  • Flutter
  • How to become a ?
  • Html
  • Instagram
  • Interview
  • IOS
  • Italy
  • jQuery
  • Laravel
  • Laravel
  • Linux
  • Mac
  • Mongodb
  • mPDF
  • Operating Systems
  • PHP
  • Question
  • React
  • Social Media
  • Topics
  • Visa
  • Vite
  • Wordpress
  • Youtube
  • Youtuber
©2025 Tutorial Wala | Powered by Tutorial Wala and Tutorial Wala