Calculate days between two dates in PHP

Calculate days between two dates in PHP

How to Calculate days between two dates in PHP (PHP में दो तिथियों के बीच दिनों की गणना कैसे करें)-

Calculate days between two dates is very easy task in PHP web scripting language given as examples bellow (नीचे दिए गए उदाहरण के अनुसार PHP वेब स्क्रिप्टिंग भाषा में दो तिथियों के बीच दिनों की गणना करना बहुत आसान कार्य है) –

Example – 1 : In this example, the days are calculated from the current date.

  • The time() function returns the current time as a Unix timestamp (time() फ़ंक्शन वर्तमान समय को यूनिक्स टाइमस्टैम्प के रूप में लौटाता है).
  • The strtotime() function is PHP predefined function which is parse an English textual datetime into a Unix timestamp (no. of seconds since January 1 1970 00:00:00 GMT) (strtotime() फ़ंक्शन PHP पूर्वनिर्धारित फ़ंक्शन है जो एक अंग्रेजी पाठ्य दिनांक और समय को यूनिक्स टाइमस्टैम्प (1 जनवरी 1970 00:00:00 GMT से सेकंड की संख्या) में पार्स करता है).
  • The round() function returns a floating-point number (UP to the nearest integer).
<?php
date_default_timezone_set('Asia/Kolkata'); // set time zone of India (GMT+5:30)

$currentDate = time(); // convert current date & time to Unix timestamp
$yourDate = strtotime("2020-04-01"); // from date

$dateDiff = $currentDate - $yourDate; // no. of seconds
$totalDays = round($dateDiff / (60 * 60 * 24)); // return UP to the nearest integer

echo "Total Days = ".$totalDays;
?>

Output : Total Days = 756

Example – 2 : In this example, the days are calculated between two dates (इस उदाहरण में, दिनों की गणना दो तिथियों के बीच की जाती है).

<?php
date_default_timezone_set('Asia/Kolkata'); // set time zone of India (GMT+5:30)

$firstDate = strtotime("2020-04-01"); // first date
$secondDate = strtotime("2020-04-30"); // second date

$dateDiff = $secondDate - $firstDate; // no. of seconds
$totalDays = round($dateDiff / (60 * 60 * 24)) + 1; // including one day

echo "Total Days = ".$totalDays;
?>

Output : Total Days = 30

What are the differences between echo and print…

Calculate days between two dates in PHP

Tags: Calculate days between two dates in PHP, Calculate days between two dates with examples, How to Calculate days between two dates in PHP, PHP date_diff() Function

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *