1

I have a customer form in two date fields. Actually I find the days between in customer_birthday date and customer_marriage date. I've tried this code to fulfill my problem but its doesn't work. where I am wrong.

My controller code

$date1 = $_POST['customer_birth']; $date2 = $_POST['customer_marriage']; $datediff = $date1 - $date2; echo floor($datediff / (60 * 60 * 24)); 

it gives me output: 0

4
  • What's value of $date1 and $date2? Commented Jun 24, 2017 at 6:50
  • FYI not all days are 24 hours long and not all minutes are 60 seconds long Commented Jun 24, 2017 at 6:50
  • the $date1 holds the mm/dd/yyyy format 06/22/2017 and $date2 holds 06/24/2017 Commented Jun 24, 2017 at 6:51
  • @DevendraSingh check my answer given below Commented Jun 24, 2017 at 7:26

7 Answers 7

4

use date_diff() function of php to calculate the diffrence between two dates

<?php $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);//OP: +272 days ?> 

You can check the mannual here PHP date_diff()

Sign up to request clarification or add additional context in comments.

Comments

2

Best way you can follow below solution.

$date1 = date("Y-m-d",strtotime($_POST['customer_birth'])); $date2 = date("Y-m-d",strtotime($_POST['customer_marriage'])); $diff = abs(strtotime($date2) - strtotime($date1)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); 

Let me know if it not works.

1 Comment

@DevendraSingh: Glad to help you out from this hurdle.
2

Your solution is....

$date1 = date_create('2015-07-31'); $date2 = date_create('2016-07-31'); $diff = date_diff($date1,$date2); $days = $diff->format("%a")+1; 

1 Comment

%a is the key.
1

You can also do this code

$datetime1 = date_create($user['datee']); $datetime2 = date_create($user['date1']); $interval = date_diff($datetime1, $datetime2); $x = @($interval->format('%R%a days')+1); 

Comments

0

You can do with compare timestamp of times:

$date1 = '06/22/2017'; $date2 = '06/24/2017'; $datediff = (strtotime($date2) - strtotime($date1)); echo floor($datediff / (60 * 60 * 24)); 

Comments

0

This should helps you:

$date1 = strtotime($date1); $date2 = strtotime($date2); $datediff = $date1 - $date2; $no_of_days = floor($datediff / (60 * 60 * 24)); var_dump($no_of_days ); 

Comments

0

Try this

$from_data = date('m',strtotime('11-10-2022 12:12:00')); $to_date = date('m',strtotime('13-10-2022 10:12:0')); $month_difference = $to_date - $from_date; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.