Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:16:11+00:00 2026-06-11T06:16:11+00:00

I checked and this is not a duplicate. I have this function $since =

  • 0

I checked and this is not a duplicate. I have this function

$since = date('Y-m-d H:i:s', strtotime($since));        
        $date = new DateTime($since);
        $interval = $date->diff(new DateTime('now'));
        return $interval->format('%y years, %m months, %d days, %h hours and %i minutos ago %s seconds ago');

But of course, I get a date like “5 years, 2 months, 5 days …. “

What I want to get is just one part of the interval. Meaning, I dont want to get “0 years, 0 months, 0 days”, but lets say “3 minutes” or “5 days” etc…

I’m new with DateTime but didn’t find a function in the php.net site that did that. Is there one or I have to create one by myself? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:16:12+00:00Added an answer on June 11, 2026 at 6:16 am

    This is what I came up with:

    <?php
    
    /**
     * Return formatted interval where only the largest unit gets displayes.
     * @param DateInterval $interval
     *
     * @return string
     */
    function format_properly(DateInterval $interval) {
        if ($interval->y !== 0) {
            return $interval->format("%y years");
        }
        if ($interval->m !== 0) {
            return $interval->format("%m months");
        }
        if ($interval->d !== 0) {
            return $interval->format("%d days");
        }
        if ($interval->h !== 0) {
            return $interval->format("%h hours");
        }
        if ($interval->i !== 0) {
            return $interval->format("%i minutes");
        }
        if ($interval->s !== 0) {
            return $interval->format("%s seconds");
        }
        return "0 seconds";
    }
    
    $date = new DateTime("now");
    $date->sub(new DateInterval("PT2H5M")); //Now - 2 hours and 5 minutes
    
    $now = new DateTime("now");
    
    $interval = $date->diff($now);
    
    echo format_properly($interval);
    var_dump($interval);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have checked this similar question, but the suggestions did not solve my problem:
I have a 2884765579 bytes file. This is double checked with this function, that
I have the following code: function toggleChecked(status) { $(.checkbox).each( function() { $(this).attr(checked,status); }) }
PreNote: This is not a duplicate. I've checked several questions and mine is different
jQuery(.single_writing).each(function(){ jQuery(this).click(function(){ jQuery(#thisTickets).val(); jQuery(#my_iframe).contents().find('.ticket_id').each(function(){ jQuery(this).click(function(){ if (jQuery(this).attr(checked)==true){ jQuery(#thisTickets).val(jQuery(#thisTickets).val()+jQuery(this).attr(id)+, ); } }); }); }) });
When I bind a function to a checkbox element like: $(#myCheckbox).click( function() { alert($(this).is(:checked));
Here is my code: $(#myCheckBox).click(function () { if (!$(this).is(':checked')) { $('#txtDateFrom').attr('readonly', true); $('#txtDateTo').attr('readonly', true);
This is my current scenario: I have checked in my grails app code +
Possible Duplicate: Using ApplicationSettings to store Checked property for WinForms RadioButtons I have three
This post is a duplicate from superuser.com, but since I had no answers, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.