Calulate day time hours and seconds with php

  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

Hi everyone,

Do anyone knows how to code calculation between two dates and echo out the result in days, hours, and seconds in php? Something like a countdown
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

What have you tried? Post your code so we can take a look at what might be wrong.
  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

Here's what I have so far

Code: [ Select ]
<?php
$date = mktime(0, 0, 0, 2, 10, 2007) ;
$today = time () ;
$difference =($date-$today) ;
$days =(int) ($difference/86400) ;
echo ("Our event will occur in $days days");
?>
  1. <?php
  2. $date = mktime(0, 0, 0, 2, 10, 2007) ;
  3. $today = time () ;
  4. $difference =($date-$today) ;
  5. $days =(int) ($difference/86400) ;
  6. echo ("Our event will occur in $days days");
  7. ?>
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

I like using the datetime class in php 5

very basic example
PHP Code: [ Select ]
 
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('Our event will occur in %a day(s)');
?>
 
 
  1.  
  2. <?php
  3. $datetime1 = new DateTime('2009-10-11');
  4. $datetime2 = new DateTime('2009-10-13');
  5. $interval = $datetime1->diff($datetime2);
  6. echo $interval->format('Our event will occur in %a day(s)');
  7. ?>
  8.  
  9.  
  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

ScottG Thank for the tip but how would I get it to echo out days, hours, and seconds?
  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

I did some research online and this is what I came up with.
Code: [ Select ]
<?php $date = strtotime("November 15, 2013 2:00 PM");                        
$remaining = $date - time();
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left to reach our goal target";
?>
  1. <?php $date = strtotime("November 15, 2013 2:00 PM");                        
  2. $remaining = $date - time();
  3. $days_remaining = floor($remaining / 86400);
  4. $hours_remaining = floor(($remaining % 86400) / 3600);
  5. echo "There are $days_remaining days and $hours_remaining hours left to reach our goal target";
  6. ?>
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

short codes
%a = All days including months and years
%d = days without leading zeros
%h = hours without leading zeros
%i = minutes without leading zeros
%m = months without leading zeros
%r = minus sign if difference is negative
%s = seconds without leading zeros
%y = years without leading zeros

%D = days with leading zeros
%H = hours with leading zeros
%I = minutes with leading zeros
%M = months with leading zeros
%R = minus sign if difference is negative and Plus sign if positive
%S = seconds with leading zeros
%Y = years with leading zeros

With short codes
PHP Code: [ Select ]
$datetime1 = new DateTime('2009-02-11 07:10:30');
$datetime2 = new DateTime('2020-10-13 08:15:45');
$interval = $datetime1->diff($datetime2);
echo $interval->format('There are %y years, %m months, %d days, %h hours, %i minutes, and %s seconds left to reach our goal target');
echo '<br/>';
echo $interval->format('There are %Y years, %M months, %D days, %H hours, %I minutes, and %S seconds left to reach our goal target');
 
  1. $datetime1 = new DateTime('2009-02-11 07:10:30');
  2. $datetime2 = new DateTime('2020-10-13 08:15:45');
  3. $interval = $datetime1->diff($datetime2);
  4. echo $interval->format('There are %y years, %m months, %d days, %h hours, %i minutes, and %s seconds left to reach our goal target');
  5. echo '<br/>';
  6. echo $interval->format('There are %Y years, %M months, %D days, %H hours, %I minutes, and %S seconds left to reach our goal target');
  7.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

With your date in mind you can do this
PHP Code: [ Select ]
$datetime1 = new DateTime('now');
$datetime2 = new DateTime('November 15, 2013 2:00 PM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('There are %y years, %m months, %d days, %h hours, %i minutes, and %s seconds left to reach our goal target');
 
  1. $datetime1 = new DateTime('now');
  2. $datetime2 = new DateTime('November 15, 2013 2:00 PM');
  3. $interval = $datetime1->diff($datetime2);
  4. echo $interval->format('There are %y years, %m months, %d days, %h hours, %i minutes, and %s seconds left to reach our goal target');
  5.  


OUTPUT
Code: [ Select ]
There are 0 years, 0 months, 16 days, 4 hours, 11 minutes, and 2 seconds left to reach our goal target
  • Dan030981
  • Novice
  • Novice
  • User avatar
  • Posts: 23
  • Loc: Mass

Post 3+ Months Ago

ScottG

Very helpful thanks just what I was looking for to get done :)

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 115 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.