|
-
Jul 22nd, 2010, 02:28 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Date and Days Diff
Hi everyone,
Sorry for disturbing again.
Could you please tell me how can i get correct difference between Hours and dates?
-
Jul 22nd, 2010, 02:39 AM
#2
Re: Date and Days Diff
how are you storing the date?
-
Jul 22nd, 2010, 02:52 AM
#3
Thread Starter
Fanatic Member
Re: Date and Days Diff
 Originally Posted by kows
how are you storing the date?
I want php code for getting this difference Kows.
just difference like 3 days, 4 days
and hours difference like 2 hours, 5 hours like this. and have to store these values in mysql table in a Int field
-
Jul 22nd, 2010, 10:10 AM
#4
Re: Date and Days Diff
 Originally Posted by chunk
I want php code for getting this difference Kows.
just difference like 3 days, 4 days
and hours difference like 2 hours, 5 hours like this. and have to store these values in mysql table in a Int field
I understand what you want, but you have to tell me how you're storing these dates first. Are the dates Unix timestamps? Are they dates being stored in a database? Are they inputted by a user? There is no one way to deal with dates.
I could just link you to DateTime::diff(), but the chances that you have PHP 5.3 installed on a shared hosting account is fairly slim at this point.
Unix timestamps are stored in seconds, so a simple calculation can be made: differenceInSeconds = newerDate - olderDate. You could then convert the differenceInSeconds to minutes (divide by 60), hours (divide by 3600 [60 * 60]), or days (divide by 86400 [60 * 60 * 24]).
-
Jul 22nd, 2010, 04:15 PM
#5
Thread Starter
Fanatic Member
Re: Date and Days Diff
i got its solution 
Code:
<?php
/**
* Calculating the difference between two dates
* @author: Elliott White
* @author: Jonathan D Eisenhamer.
* @link: http://www.quepublishing.com/articles/article.asp?p=664657&rl=1
* @since: Dec 1, 2006.
*/
if( function_exists( 'date_default_timezone_set' ) )
{
// Set the default timezone to US/Eastern
date_default_timezone_set( 'US/Eastern' );
}
// Will return the number of days between the two dates passed in
function count_days( $a, $b )
{
// First we need to break these dates into their constituent parts:
$gd_a = getdate( $a );
$gd_b = getdate( $b );
// Now recreate these timestamps, based upon noon on each day
// The specific time doesn't matter but it must be the same each day
$a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
$b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
// Subtract these two numbers and divide by the number of seconds in a
// day. Round the result since crossing over a daylight savings time
// barrier will cause this time to be off by an hour or two.
return round( abs( $a_new - $b_new ) / 86400 );
}
// Prepare a few dates
/*
$date1 = strtotime( '12/3/1973 8:13am' );
$date2 = strtotime( '1/15/1974 10:15pm' );
$date3 = strtotime( '2/14/2005 1:32pm' );
*/
$date1 = strtotime( '12/13/2010' );
$date2 = strtotime( '12/19/2010' );
$date3 = strtotime( '12/23/2010' );
// Calculate the differences, they should be 43 & 11353
echo "<p>There are ", count_days( $date1, $date2 ), " days.</p>\n";
echo "<p>There are ", count_days( $date2, $date3 ), " days.</p>\n";
?>
<?php
$to_time=strtotime("2008-12-13 11:42:00");
$from_time=strtotime("2008-12-13 12:21:00");
echo round(abs($to_time - $from_time) / 60,2)." minute";
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|