How to convert datetime from time ago in php

How to convert datetime from time ago in php

Simple php function for converting datetime format to Time Ago format in php

Create a function get_timeago()

 function get_timeago($time) {
    // Calculate difference between current
    // time and given timestamp in seconds
    $curtim=date_timestamp_get(date_create(date('Y-m-d H:i:s')));
    $diff = $curtim - strtotime($time);
    // Time difference in seconds
    $sec = $diff;
    // Convert time difference in minutes
    $min = round($diff / 60 );
    // Convert time difference in hours
    $hrs = round($diff / 3600);
    // Convert time difference in days
    $days = round($diff / 86400 );
    // Convert time difference in weeks
    $weeks = round($diff / 604800);
    // Convert time difference in months
    $mnths = round($diff / 2600640 );
    // Convert time difference in years
    $yrs = round($diff / 31207680 );
      
    // Check for seconds
    if($sec <= 60) {
        return "$sec seconds ago";
    }
      
    // Check for minutes
    else if($min <= 60) {
        if($min==1) {
            return "one minute ago";
        }
        else {
            return "$min minutes ago";
        }
    }
      
    // Check for hours
    else if($hrs <= 24) {
        if($hrs == 1) { 
            return "an hour ago";
        }
        else {
            return "$hrs hours ago";
        }
    }
      
    // Check for days
    else if($days <= 7) {
        if($days == 1) {
            return "Yesterday";
        }
        else {
            return "$days days ago";
        }
    }
      
    // Check for weeks
    else if($weeks <= 4.3) {
        if($weeks == 1) {
            return "a week ago";
        }
        else {
            return "$weeks weeks ago";
        }
    }
      
    // Check for months
    else if($mnths <= 12) {
        if($mnths == 1) {
            return "a month ago";
        }
        else {
            return "$mnths months ago";
        }
    }
      
    // Check for years
    else {
        if($yrs == 1) {
            return "one year ago";
        }
        else {
            return "$yrs years ago";
        }
    }
}
}

Then call function like this

echo  get_timeago('2021-07-01 17:05:25');


Leave a reply
Latest Posts
Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case
Whatever the Bishnoi community says, it is difficult for Salman Khan to apologize in the black buck case

Salman Khan has been advised by the Bishnoi community of Rajasthan to apologize in the blackbuck poaching case - but will the matter end if Salman Khan does so? And will Salman Khan want to take this path?

India Mourns the Loss of Beloved Industrial Icon, Padma Vibhushan Ratan Tata
India Mourns the Loss of Beloved Industrial Icon, Padma Vibhushan Ratan Tata

Ratan Tata will be cremated with full state honours after passing away on Wednesday at the age of 86 in a Mumbai hospital. In addition, Eknath Shinde, the chief minister of Maharashtra, declared a day of mourning for the renowned philanthropist and industrialist on Thursday.

Ratan Tata: The Visionary Who Shaped India's Industrial Future
Ratan Tata: The Visionary Who Shaped India's Industrial Future

Ratan Tata, one of the most respected business leaders in India, has left an indelible mark on the nation’s economy through his visionary leadership and values-driven approach. Born on December 28, 1937, into the Tata family, Ratan Tata is the great-grandson of Jamsetji Tata, the founder of the Tata Group, one of India’s oldest and most prestigious conglomerates. Over the decades, Ratan Tata has taken the group to unprecedented heights, earning admiration globally for his business acumen and ethical leadership.

2024 Ballon d'Or Nominees | Top Football Stars Revealed
2024 Ballon d'Or Nominees | Top Football Stars Revealed

The latest news about the 2024 Ballon d'Or nominees. Updates and details on the top football stars. The most current information on football awards at Sportxeber.az.