show an alert three days later from the beginning date - php

I need to make a function which will add to the beginning date 3 days and if it's 3 then it shows alert.
$beginDate = 2016-07-29 17:14:43 (this one is from sql table format)
$dateNow = date now
if $beginDate + 3 days (72h) is >= $dateNow then echo "It's been three days from..."
It seems to be simple but I have hard time to make it work with strtotime() and date() functions. How to code this in PHP?

You can do something like this
$beginDate = "2016-07-29 17:14:43";
$beginDate = strtotime($beginDate);
echo "beginDate ".date('Y-m-d h:i:s', $beginDate);
$beginDate3Day = strtotime("+3 day", $beginDate);
$beginDate3Days = date('Y-m-d h:i:s', $beginDate3Day);
echo "<br />$beginDate3Days ";
$today = date("Y-m-d h:i:s");
if (beginDate3Days > $today )
echo "It's been three days from..."

$beginDate = 2016-07-29 17:14:43 (this one is from sql table format)
$dateNow = date("Y-m-d H:i:s")
if (date('Y-m-d H:i:s', strtotime($beginDate.' +3 day')) >= $dateNow){
echo "3 days to be passed from begin date."
}else{
echo "3 days passed from begin date."
}

Related

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)));
}

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

stringtotime from Unix timestamp

I am doing the following in my php script and I am not seeing the results I though I would:
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', strtotime($current_date_num));
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', strtotime($current_date_num) - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
This is my output and Only the END DATE is showing what I would expect.
1322673564 BEFORE DATE HOUR
1969-12-31 07:00:00 AFTER DATE
1969-12-31 01:00:00 CURRENT
2011-12-01 12:19:24 END DATE
What I thought would happen is I get the Unix timestamp and then use strtotime and get the current time and then the current time minus 6 hours and finally the time 24 hours from now. Not sure how I am messing this up so bad?
You are applying strtotime() two times too many. One time in line 4:
strtotime($current_date_num)
remove that, and the strtotime() call in line 7.
strtotime takes a human-readable time and parses it into a timestamp. As you already have a timestamp, the strptime calls are generating bogus data.
$current_date_num = strtotime("now");
echo $current_date_num ." BEFORE DATE HOUR<br/>";
$new_current = date('Y-m-d h:i:s', $current_date_num);
echo $new_current . " AFTER DATE<br/>";
$current_date_num = date('Y-m-d h:i:s', $current_date_num - 60 * 60 * 6);
echo $current_date_num ." CURRENT<br/>";
$end_date = strtotime("+1 day");
$end_date = date("Y-m-d h:i:s",$end_date);
echo $end_date ." END DATE";
1322674409 BEFORE DATE HOUR
2011-11-30 06:33:29 AFTER DATE
2011-11-30 12:33:29 CURRENT
2011-12-01 06:33:29 END DATE

Categories