Time deference between two date("F j, Y") dates in php - php

i have a problem to get month difference between two dates in months.
$d1 = date_create('January 1, 2013');
$date = date("F j, Y");
$d2 = date_create($date);
$dif = date_diff($d1, $d2);
//echo $dif->format('%y years');
echo $dif->format('%m months');
It shows months but not the whole difference in months. I just want the diff in months between dates.

%m only shows up to 11 months. After that years are populated. If you want total months you need to figure in years and do some math:
$d1 = date_create('January 1, 2013');
$d2 = date_create();
$dif = date_diff($d1, $d2);
echo ($dif->format('%m') + $dif->format('%y') * 12) . ' months';
Demo
FYI, the above solution removes unnecessary code. If you want today's date you just don't pass any parameters to date_create().

Related

i want convert integer number to date in year in php [duplicate]

This question already has answers here:
PHP: Convert Day of Year to Day of Month and Month
(2 answers)
Closed 1 year ago.
I want to convert the number of year to date
for example in number 76 in 2021 is 17-3
$d = 76;
$x = substr($d, 0, 4) .'-'. substr($d, 4,2) .'-'. substr($d,6,2) .' '. substr($d,8,2) .':'. substr($d,10,2) .':'. substr($d,12,2);
echo $x;
You can use add,
$date = new DateTime("2021");
$days = 76;
$date->add(new DateInterval("P${days}D"));
echo $date->format("Y-m-d H:i:s");
strtotime is the most obvious solution for your task:
$result = date('Y-m-d H:i:s', strtotime('2021-01-00 +76 day'));
The number 76 for the date 17-3 is the day of the year starting with 1. Some solutions with DateTime.
For the current year:
$dayOfYear = 76; //starting from 1
$dateTime = date_create('Jan 1 +'.($dayOfYear-1).' Days');
echo $dateTime->format('Y-m-d H:i:s'); //2021-03-17 00:00:00
Alternatively, the solution from here, changed for the day of the year from 1 and to get the time 00:00.
$dateTime = DateTime::createFromFormat('H:i:sz', "00:00:00".($dayOfYear-1));
If you want to use a fixed year:
$dateTime = date_create('2021-01-01 +'.($dayOfYear-1).' Days');

strtotime('-1 month') returning wrong date if month have 31 days

I'm trying make a function to return the exact date of previous months.
That is a example of my code:
// Dates in TimeStamp
$ts_now = strtotime('now');
$ts_month1 = strtotime("-1 month", $ts_now);
$ts_month2 = strtotime("-2 month", $ts_now);
$ts_month3 = strtotime("-3 month", $ts_now);
// Dates Formated
$date_now = date('Y-m-d', $ts_now);
$date_month1 = date('Y-m-d', $ts_month1);
$date_month2 = date('Y-m-d', $ts_month2);
$date_month3 = date('Y-m-d', $ts_month3);
//Output
echo $date_now; //2020-04-30
echo $date_month1; //2020-03-30
echo $date_month2; //2020-03-01
echo $date_month3; //2020-01-30
The problem is in $date_month2 that represents February, the output is 2020-03-01 instead 2020-02-29 and I suppose that problem will happen in months who have 30 days when present date have 31 days.
What is the best way to resolve that?
As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.
function getMonthsAgo(int $n): string {
$date = new DateTime();
$day = $date->format('j');
$date->modify('first day of this month')->modify('-' . $n . ' months');
if ($day > $date->format('t')) {
$day = $date->format('t');
}
$date->setDate($date->format('Y'), $date->format('m'), $day);
return $date->format('Y-m-d');
}
// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);
//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;
Output:
2020-04-30
2020-03-30
2020-02-29
2020-01-30

Count up from date and display weeks and days only - PHP

How can you count up from a date, May 7, 2016 and just display the number of weeks and days (5 Weeks 1 Day) from that date with the least amount of code?
Use like this
<?php
$date = "May 07, 2011";
$date = strtotime($date);
$week=5;
$day=1;
$days=(5*7)+1;
$date = strtotime("+".$days." day", $date);
echo date('M d, Y', $date);
?>
Use the PHP DateTime class.
$lastDate="2016-10-10";
$date1 = new DateTime("2016-05-07");
$date2 = new DateTime($lastDate);
$difference = $date1->diff($date2);
echo "difference " .floor($difference->days/7)." weeks, ".($interval->days%7)." days ";
Get the number of weeks by dividing and modulus by 7 as above

Adding three months to a date in PHP

I have a variable called $effectiveDate containing the date 2012-03-26.
I am trying to add three months to this date and have been unsuccessful at it.
Here is what I have tried:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
and
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");
What am I doing wrong? Neither piece of code worked.
Change it to this will give you the expected format:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.
$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;
I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version
You need to convert the date into a readable value. You may use strftime() or date().
Try this:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;
This should work. I like using strftime better as it can be used for localization you might want to try it.
Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:
$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)
The following should work,Please Try this:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
Following should work
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo date("Y-m-d",$d); // This will print **2015-06-25**
Add nth Days, months and years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
As of PHP 5.3, DateTime along with DateInterval could be a feasible option to achieve the desired result.
$months = 6;
$currentDate = new DateTime();
$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));
echo $newDate->format('Y-m-d');
If you want to subtract time from a date, instead of add, use sub.
Here are more examples on how to use DateInterval:
$interval = new DateInterval('P1Y2M3DT4H5M6S');
// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.
$interval = new DateInterval('P2W');
// This creates an interval of 2 weeks (which is equivalent to 14 days).
$interval = new DateInterval('PT1H30M');
// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).
The following should work, but you may need to change the format:
echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));
public function getCurrentDate(){
return date("Y-m-d H:i:s");
}
public function getNextDateAfterMonth($date1,$monthNumber){
return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1)));
}

subtract time. Day is same [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtract time in PHP
How can i subtract time? the day remains the same. i just need to subtract two time.
Date = 2011-04-26
Starttime = 12:39:53
Endtime = 14:34:28
now i need to calculate the time difference between Endtime and Starttime.
Thanks
Try this one:
<?php
$lasttime = strtotime("-67 minutes"); // for instance
$currentdate = strtotime("now");
$lastact = date("Y-m-d g:i:s a", $lasttime);
$now = date("Y-m-d g:i:s a", $currentdate);
$daycheck = date("d", $now) - date("d", $lastact);
$minutecheck = date("i", $now) - date("i", $lastact);
$difference = $currentdate - $lasttime;
?>

Categories