How to add month from current date in PHP [duplicate] - php

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 7 years ago.
Hi I'm still new in PHP and currently I want to make system about Employee Allowance and have no idea how to start calculate months in php .For example Steve should get allowance for 4 months from now and database stored the result of calculation, so far the coding is:
?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
Months input is from user and stored in database.

I'm not sure I follow your 40 days versus 4 months, but I think you want something like this:
echo date('Y-m-d', strtotime("Now +40 days"));

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.

Add an hour to a time value on php [duplicate]

This question already has answers here:
Add 'x' number of hours to date
(13 answers)
Closed 7 years ago.
I have a form that people are filling in and adding appointment times to, im then inputting the form into a sql db. Im then making a sheet and that shows the appointment slot. All that I need to to is add 1 hour to the appointment slot time. I have a variable call $time that is the appointment time I have tried the following:
<?php
$time=strtotime("+3600");
$final=date("H:i",$time);
echo $final;
but it comes back with all different times any ideas how to make this work?
$time=strtotime("+1 hour");
$time=strtotime("+60 minutes");
Strtotime means: String to time, very useful method when working with dates in PHP.
Or like below answer:
$time = time() + 3600;
Note that you should avoid calculation with strings, that is: A number between quotes. (string)"3600" vs (int)3600

Extended hours in php [duplicate]

This question already has answers here:
Calculate time elapsed in php - spans over 24 hours
(2 answers)
Closed 9 years ago.
I have been searching for a while now regarding on how to format hours in php that will show 000:00:00 instead of 00:00:00.
I need this to calculate the duration of records with start and end Date with a datetime data type in mysql.
The problem with my current format ('H:m:s') is it only count 24 hours and when the duration goes higher than that, it will show 01:00:00 instead of 25:00:00.
Try TIMEDIFF() function in MySQL
Example:
SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
'2008-12-30 01:01:01.000002');
-> '46:58:57.999999'

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

Categories