Obtain a month is a few days? [duplicate] - php

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()

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;

Convert days to weeks and month [duplicate]

This question already has answers here:
Convert Days to Year,Month,Day Format
(2 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
1st method: I've got x days and I want to convert it to weeks and month via php. For example 24 days:
3 weeks 3 days
or 45 days:
that's where my problem starts. How can I know that 45 days is how many month including month differentials?
2nd method: I've got two dates, for example 2020-05-13 and 2020-07-14. Can I calculate month, weeks, and days somehow from these?
I don't have any code since I don't even know how to start it.

calculate last date, given start date and month PHP [duplicate]

This question already has answers here:
Adding three months to a date in PHP
(11 answers)
Closed 6 years ago.
There is a start date(let it be 16/02/2016) and months(let it be 3 months) given to me.
how do I calculate the end date?
This will help you
$myDate = "16-02-2016";
$final_date = date('Y-m-d', strtotime("+3 months", strtotime($myDate)));

How to calculate days between 2 month? [duplicate]

This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Finding the number of days between two dates
(34 answers)
Closed 8 years ago.
I want to calculate days between 2 month.1 date is set that is 01-03-2014 and second date will be given by user.This may be any date for example 14-05-2014.Now i want to calculate days between these 2 dates?
Thanks
You can use DateTime for this, and do a simply calculation. See DateTime:diff
<?php
$start= new DateTime('14-05-2014');
$end= new DateTime('01-03-2014');
$interval = $start->diff($end);
echo $interval->format("%a total days"); //Output: 74 total days
https://eval.in/236630

Convert Month Year function to Time Ago PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP How to find the time elapsed since a date time?
I have the following which produces MONTH YEAR.
<?php echo date('F Y', strtotime($video['Video']['posted_date'])); ?>
I'd like this to output to "Time" ago. i.e 3 hours ago, 6 months ago, 2 years ago.
Any help will be very much appreciated.
Thanks
Andy
$now = new DateTime(date("Y-m-d H:i:s"));
$videoPosted = new DateTime($video['Video']['posted_date']);
$interval = $now->diff($videoPosted);
echo $interval->format('%h hours, %m month and %d days ago');
Use only if PHP version > 5.3
If you use php 5.3+, you are looking for DateInterval::format()
this will help you to calculate date difference: How to calculate the difference between two dates using PHP?
and after that you can convert the result using strtotime()

Categories