How can we know the number of days between two given dates
using PHP?
How can we know the number of days between two given dates
using MySQL?

Answers were Sorted based on User's Feedback



How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / abdulgafoor

Using PHP

$date1 = date("Y-m-d");
$date2= "2005-5-15";
$nodays = (strtotime()-strtotime())/(60*60*24);

echo "no of days are $nodays ";

Using mySql :

mysql>SELECT DATEDIFF(NOW(),'2005-5-15');
The output will give you the exact number of days.

Is This Answer Correct ?    12 Yes 4 No

How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / shaik abdul raheem

SELECT DATEDIFF('2006-04-01','2006-03-01')

Is This Answer Correct ?    6 Yes 0 No

How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / jitendra jhariya and sandeep s

function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1],
$date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1],
$date_parts2[2]);
return $end_date - $start_date;
}Now let us see how we use this function:$date1="07/11/2003";
$date2="09/04/2004";print "If we minus " . $date1 . " from "
. $date2 . " we get " . dateDiff("/", $date2, $date1) .
".";which generates If we minus 07/11/2003 from 09/04/2004
we get 421.

Is This Answer Correct ?    6 Yes 1 No

How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / vipul dalwala

php
-------------------------------------------------------
$noofdays = ceil(((strtotime($endDate) - strtotime
($startDate)) / (60*60*24)))


Mysql
-------------------------------------------------------
SELECT ((UNIX_TIMESTAMP('2007-10-01') - UNIX_TIMESTAMP
('2007-09-04'))/(60*60*24)) AS dateDiff;

Is This Answer Correct ?    5 Yes 4 No

How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / vinay sachan

<?PHP
// different between two dates by date_diff() function:-
$datetime1 = date_create('2010-01-01');
$datetime2 = date_create('2012-1-15');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a days')."<br>";
//+744 days
echo $interval->format('%y-%m-%d days')."<br>";
//2-0-14 days
// different between two dates by strtotime() function:-
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24)) ."<br>";
//744
?>

for more PHP questions and their best answers and PHP notes at http://onlinephpstudy.blogspot.com/

Is This Answer Correct ?    1 Yes 0 No

How can we know the number of days between two given dates using PHP? How can we know the number..

Answer / indira majumder

<?php
$date1 = date('y-m-d');
$date2 = '2007-12-7';
$days = (strtotime($date2)-strtotime($date1))/(60*60*24);
echo "Number of Days since '2007-12-7': $days";

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More PHP Interview Questions

What is binary safe function in php?

0 Answers  


How can you insert javascript in php code?

13 Answers   TCS,


What are the Advantages and Application Areas of PHP?

0 Answers  


How can we check the value of a given variable is a number?

0 Answers  


how set session expire time in php?

4 Answers  






How to swap two variables without using 3rd temp variable.

0 Answers  


What is the difference between php4 and php5?

0 Answers  


How to get the value of current session id?

0 Answers  


1. Create student database. 2. First page should display the students available in the database. There should be add, edit and delete buttons. 3. There should be option to search students by name, code, date of joining, department or combination of these. 4. Should have an add/edit screen. Add and Edit should be handled in the same page. 5. Delete should ask for confirmation before deleting the actual record. 6. Validation should be done in JavaScript as well as php.

0 Answers   Zonex,


What is the scope of a variable defined in a function?

0 Answers  


How can I loop through the members of an array?

1 Answers   Rushmore Consultancy,


How to get the number of characters in a string?

0 Answers  


Categories