PHP get 31 days distance from starting date - php

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

Related

How can I get 10 days future date from today in Laravel (PHP)

I'm trying to get a future date from today using PHP.
like date('dS M, Y (D)') returns 21st Jan, 2021 (Thu) (Today)
How can we get a future date from today!
You could use the superb Carbon package by installing it via composer.
Then in your code you can run add days like this
Entering the date piece by piece
$dt = Carbon::create(2021, 1, 22, 0);
echo $dt->addDays(10); // adding 10 days from today on
Using now()
echo Carbon::now()->addDays(10); // adding 10 days from today on
You could use PHP's DateTime class:
$dt = new DateTime();
$dt->modify( '+10 days' );
echo $dt->format( 'dS M, Y (D)' );
https://www.php.net/manual/en/book.datetime.php
You can use it that way. You don't need any package for this job.
$tenDaysLater = date ('Y-m-d', strtotime ('+10 day'));
try this code
$date = date('Y-m-d');
$addDay= strtotime($date . "+10 days");
echo date('Y-m-d', $addDay);
You could do this:
$datetime = Carbon::now()->addDays(30);

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.

Converting separate month, day and year values into a timestamp

I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
How can I give this to strtotime() in a way it can convert it to a timestamp?
why convert string to date when you already know year month and date.
use setDate funtion
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Given the variables $year $month $day $hour $minute you can do:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with ", never in this case with '.
UPDATE (thanks to comments of #Clockwork and #Tadeck):
In case there is a variable $timeofday that represents the time of day (i.e. AM or PM),
then you can parse it this with:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
Is strtotime the best tool for this job? What about mktime()?
$time = mktime($hour, $minute, 0, $month, $day, $year);
You can provide it to function strtotime() in many ways, as mentioned in documentation. Some examples include:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
10/Oct/2000:13:55:36 -0700,
2008:08:07 18:11:31,
2008-08-07 18:11:31,
2008-07-01T22:35:17.02,
2008-07-01T22:35:17.03+08:00,
20080701T22:38:07,
20080701T9:38:07,
20080701t223807,
20080701T093807,
2008-7-1T9:3:37,
(this is really copy of the documentation)
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM"):
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
01 June 2011 11:15:00 PM
Y-m-d hh:mm will work
echo strtotime('2011-12-14 11:44 am');
cit #Pekka :)
strtotime($month."-".$day."-".$year)

get a date 6 years from now?

i want to have a date 6 years from now?
how do i do that?
<?php
$timestamp = strtotime('+6 years');
echo date('Y-m-d H:i:s', $timestamp);
?>
date_default_timezone_set('America/Los_Angeles'); //required if not set
$date = new DateTime('1/1/1981');
$date->modify('+60 year');
echo $date->format('Y-m-d');
Above is not affected by unix time stamp date range (before 1970 and after 2038).
Also you can directly compare dates with Comparison operators directly, no need convert them to Time stamp.
Requires PHP 5.3
strtotime('+6 years');
you can pass that timestamp into something like strftime();
strtotime
Still laughing about ChaosPandion's comment :)
echo strtotime ("+6 years");
should do the trick.
Your description isn't very precise, but echo date("Y-m-d", strtotime("+6 years")); might be what you need ...
189302400 is the number of seconds in 6 years.
Get the current timestamp, then add 189302400, and then convert the timestamp to a date string.

Given a time, how can I find the time one month ago

Given a time, how can I find the time one month ago.
strtotime( '-1 month', $timestamp );
http://php.net/manual/en/function.strtotime.php
In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime
We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.
// say its "2015-11-17 03:27:22"
$dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
Hope this adds some more info for this question.
<?php
$date = new DateTime("18-July-2008 16:30:30");
echo $date->format("d-m-Y H:i:s").'<br />';
date_sub($date, new DateInterval("P1M"));
echo '<br />'.$date->format("d-m-Y").' : 1 Month';
?>
PHP 5.2=<
$date = new DateTime(); // Return Datetime object for current time
$date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
echo $date->format('Y-m-d'); // To set the format
Ref: http://php.net/manual/en/datetime.modify.php
This code is for getting 1 month before not 30 days
$date = "2016-03-31";
$days = date("t", strtotime($date));
echo date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months.
I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.
I do something like this:
$current_ym = date('ym',strtotime("-15 days",$ts));

Categories