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

  • SEARCH
  • Home
  • 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 927743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:54:03+00:00 2026-05-15T19:54:03+00:00

How do I get a time countdown using PHP? I want to display something

  • 0

How do I get a time countdown using PHP?

I want to display something like 3Days 4Hours. I have a date field coming from MySQL table and want to calculate it with today’s date. As of now I have only date and not the time stored in the database, but eventually I will. So at that time, I might show as 3Days 4Hours 10Minutes 43seconds.

This is what I tried but am getting some wrong answer:

$datetime1 = new DateTime($starton);//$starton - date stored in db
$datetime2 = new DateTime(date());
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d days);

I am confused if this works based of server time or the zone where the user is coming from. Please guide me. When I have the time field, I guess I might need jQuery to show the seconds live and so the minutes too.

  • 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-05-15T19:54:04+00:00Added an answer on May 15, 2026 at 7:54 pm

    IMHO when you are thinking about comparing time, you automatically start talking Unix time. Essentially, Unix time is a count in seconds that always increments. When you have 2 Unix timestamps, then you can use simple arithmetic to figure the difference and then translate the difference into human readable form.

    Note that Unix timestamps are common in almost all programming languages, so you can choose whether to do this in PHP, MySQL or JavaScript and it could all turn out the same way. I’ll show you the PHP version.

    So you want to do this:

    1. Find the Unix timestamp for your target event
    2. Store that in the MySQL database
    3. Retrieve from the database
    4. Get current Unix timestamp
    5. Subtract to get difference
    6. Multiply/divide to get human readable format

    The PHP code:

    //Unix timestamp to Dec. 21, 2012 (midnight)
    $unix_time = mktime(0,0,0,12,21,2012); 
    //Connect to MySQL
    //"INSERT INTO table (target_timestamp) VALUES ($unix_time);"
    
    
    //----- user goes to page -----
    //Connect to MySQL
    $sql = "SELECT target_timestamp FROM table WHERE foo = bar";
    $unix_time = do_query($sql);//do_query not defined, assume it gets the timestamp
    $current_time = time();
    $diff = $unix_time - $current_time
    //$diff should be positive and not 0
    if( 1 > $diff ){
       exit('Target Event Already Passed (or is passing this very instant)');
    } else {
       $w = $diff / 86400 / 7;
       $d = $diff / 86400 % 7;
       $h = $diff / 3600 % 24;
       $m = $diff / 60 % 60; 
       $s = $diff % 60;
    
       return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found a workaround for this, override the methods for… May 16, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer PartInitializer is changed to CompositionInitializer: CompositionInitializer.SatisfyImports(this) May 16, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer You've got to go all or nothing. The plain, boring… May 16, 2026 at 8:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using asp.net 3.5 with C#. I want to create a countdown timer
I have used a jQuery script in order to have a countdown script on
I have a countdown timer in javascript that is called by a code behind
I'm using the jQuery Countdown plugin but having some trouble getting it to show
I was wondering what the best way is to calculate the difference in time
Script: http://keith-wood.name/countdown.htm Daily json response: items: { fajr: '5:23 am', sharooq: '7:23 am', dhur:
I am trying to run a NSTimer on a thread using iPhone SDK 3.0.
Whenever I try to write graphical programs (whether a game or really any GUI
I put together a quick WinForm/embedded IE browser control which logs into our company's

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.