Set start and end date for last 30 days ending today - php

I need to generate two dates in the format YYYY-MM-DD example: 2010-06-09
The end date should be today and start date should be today - 30 days.
How can I generate these 2 dates in the above format?

for the last 30 days so end date is
today and start date is today - 30
days
The strtotime is real friend:
echo date('Y-m-d', strtotime('today - 30 days'));

It's very simple with DateTime class. You can simply pass the string with the relative expression to the constuctor. When you need to get the date as a string in a specific format use format() method.
$endDate = new \DateTime();
$startDate = new \DateTime('-30 days');
// when you need to use them simple format as a string:
echo $endDate->format('Y-m-d');
echo $startDate->format('Y-m-d');

Related

Get seconds from a date to end of month PHP

Pulling a date of subscription from an sql database table we want the customer to have until the end of the month one year later to make a payment. The subscription is just some date within a month. The one year later part is easy, figuring out where the end of that month is and adding it, in seconds, to the one year part is giving me problems.
The date is stored as the number of seconds from unix ground zero. How do I find the number of seconds from that value to the end of that month? I've tried converting the date value to an actual date using m-i-Y
End of Month:
$expDate = date('m-i-Y',$row1["renewDate"]);
This works. I get the last day of that month in string form. But if I try:
$endOfMonth = strtotime($expDate);
Doesn't work....
echo'ing $expDate shows the last day of the month in string form.
echo'ing $endOfMonth returns nothing...
Thanks for any thoughts on this.
strtotime doesn't work properly with arbitrary date formats. You have two options:
Use date_create_from_format to parse custom date formats.
Convert your string to Y-m-d format which strtotime understands
automatically.
For example:
$date = date('Y-m-t', $row1["renewDate"]);
$timestamp = strtotime($date);
P.S. As mentioned in comments, you should use t instead of i.
You can play around with something like this. If the database has Epoch time then you can use the '#' symbol to convert it to a date. You can get the subscription date that way, and also the end of the subscription month date using m/t/Y. You can convert that back to DT and then to UNIX time with get Timestamp. Looks like it works for time = 1560961801.
$row1["renewDate"] = 1560961801;
$unixfromDB = $row1["renewDate"];
$date = new DateTime('#' .$unixfromDB); // your UNIX timestamp.
$subdate = $date->format( 'm/d/Y' ); // subscription date
$endmonth = $date->format( 'm/t/Y' ); // end of month date
$endmonth = DateTime::createFromFormat('m/d/Y', $endmonth);
$endmonth = $endmonth->getTimestamp(); // UNIX timestamp for end of month.
echo ($endmonth - $unixfromDB) / (60 * 60 *24); // days to end of month
Try using mktime() instead of strtotime().
<?
/* establish variables */
$now=time(); // epoch seconds right now
$expDate = date('m-t-Y', $row1["renewDate"]); //note the switch to 't' as suggested by #ehymel
$eom = mktime($expDate); // converts 'end of month' date into epoch seconds
/* results */
$secondsleft = $eom - $now; // number of seconds until the end of the month
echo $secondsleft; // return results
?>
mktime($expDate) unfortunately didn't work, at least with CentOS6.9. Even with the expdate variable it still returned current system time.
Using the date format that strtotime would recognize, Y-m-t, did work correctly. Thanks user1597430...

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

Can strtotime return the date and time of a day AFTER a given date?

I am working on generating recurring dates using PHP to process the dates. I have a $startDateTime and $endDateTime. These two variables are for the first date in the series.
If the even repeats every Tuesday I need something along the lines of
$repeatDay = "Tuesday";
$followingDay = strtotime($startDateTime. " following $repeatDay");
Using "next $repeatDay" doesn't work since I am not generating the date from todays date.
EDIT:
It seems that every five loops the time jumps forward an hour in the date. This may be because $start="2014-04-29 11:00:00" and strtotime is only converting the date correctly.
How should I convert 2014-04-29 11:00:00 to a format that strtotime understands?
$firstOccurrence=strtotime($start);
$nextOccurence=strtotime("next $day", $firstOccurrence); //Set the first recurring date for this day
while($nextOccurence<=strtotime($activeUntil)){
echo date("M d, Y H:m:i",$nextOccurence)." | ";
$nextOccurence=strtotime("next $day", $nextOccurence);
}
Maybe it's time to start working with DateTime? It's pretty complex in modyfing dates. For example, creating date time from your $start would look like this:
$start="2014-04-29 11:00:00";
$dateTime=DateTime::createFromFormat("Y-m-d H:m:i", $start);
And as you have $dateTime, you can modify it by one day:
$dateTime->modify('+1 day');
//or just
$dateTime->modify('next tuesday');
//and return it as string
echo $dateTime->format("M d, Y H:m:i");
DateTime understands everything that strtotime does, so it can improve your solution. Try it out yourself, and let me know if this helps.
strtotime() can take 2 parameters. The first is the string and the second is the base timestamp.
Try the following:
// Assuming $startDateTime is a timestamp.
$followingDay = strtotime("next $repeatDay", $startDateTime);

Find out the fourth day from a n strtotime date in PHP

I have a strtotime date in a variable. I need to find out the 4th day from that variable
$strtotime1=strtotime('06/17/2012');
I need to find out the 4th day (i.e. 06/21/2012) in strtotime format.
I have tried with:
$strtotime2=$strtotime1+4*60 * 60;
But it's not working.
You are adding 4 hours not 4 days.
It should be $strtotime2=$strtotime1+4*24*60*60;
Or
You could do like:
$strtotime2 = strtotime('+4 days', $strtotime1);
$date2 = date('m/d/Y', $strtotime2);

PHP get 31 days distance from starting date

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));

Categories