Calulate Years from two dates in MySql [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Find difference between two dates in PHP or MySQL
Bonjour! My question is how do you calculate the number of years between two dates using MySql and Php seperately? I want 2 answers: One for MySql and the other for Php.

MySQL:
YEAR(CURDATE()) - YEAR(datecolumn)
PHP:
$interval = date_diff($datetime1, $datetime2);
echo $interval->y;

Related

Total Days between two dates [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 months ago.
i am beginner at php. I want to calculate exact total number of days between two dates using php, mysql and want to show it in html form. I tried datediff but it doesn't works as it gives , diffrence not total number of days.
Date fm - 10-10-22;
Date to - 20-10-22;
Total Days - 11
add + 1 to date_diff the output will 11 be like :
<?php
$d1=date_create("2022-10-10");
$d2=date_create("2022-10-20");
$diff=date_diff($d1,$d2);
echo $result = 1 + $diff->format("%R%a days");
?>
php has inbuilt DateTime functions to get the difference. Below outputs 10 day difference.
$date = new DateTime('2022-10-20');
$next_date = new DateTime('2022-10-30');
echo $date->diff($next_date)->days;

difference between two dates with format YYYYMMDDTHHiiss [duplicate]

This question already has answers here:
How to convert date to timestamp in PHP?
(22 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 1 year ago.
I have this php code to get the difference between two dates:
$date1=date_create("01.06.2021");
$date2=date_create("05.06.2021");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a");
Result:
+4
But how can I realize this, wenn the format of $date1 is for example: 20210605T180000

Get dates from date to date in laravel [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 6 years ago.
I have two dates starting date and end date. How to get range of days from these two dates?
Thanks!
You should user DatePeriod class and example:
$period = new \DatePeriod(
new \DateTime('2010-10-01'),
new \DateInterval('P1D'),
new \DateTime('2010-10-05')
);

Obtain a month is a few days? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP iterate days of month given month and year
"Obtain all days in month with the number of months"
For example i have date 2011/12/16 this date have months 12(december) now i want with code php know that, This month(12 or december) is a few days?(29? or 31? or 30?)
How is it by PHP?
$mydate = strtotime('2011/12/16');
$daysInMonth = date('t',$mydate);
Edited to add strtotime()

how to compare dates [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to Compare Dates in php?
I have these two dates
1305004066
1305007443
I want to compare which one is later on than other. How can I do that
If it's UNIX timestamps (seconds since 1970-01-01), the largest one is the latest:
$latest = max($time1, $time2);

Categories