how to get week +4 in this day with carbon - php

I have a problem here I want to get the +4 week date from the current date using carbon, the +4 week plan will be dynamic depending on the input entered by the user, how do I make it, I've tried using this code but it's time to step back
$dt = Carbon::now();
dd($dt->week(4)->format('Y-m-d'));

Check Carbon docs, you may use addWeeks():
$dt = Carbon::now();
dd($dt->addWeeks(4)->format('Y-m-d'));
The week() method you've used, sets the week number using given first day of week and first day of year included in the first week.

I am not sure to understand your question, but I guess you just have to use :
$dt = Carbon::now();
$dt->addWeeks(4);
dd($dt->format('Y-m-d');

You don't need carbon for such simple tasks.
With DateTime
echo date_create('+4 weeks')->format("Y-m-d");
or with date and strtotime
echo date("Y-m-d",strtotime('+4 weeks'));

Related

Add Days to PHP Variable

I have 2 variables in PHP. One is getting today's date and adding 1 month to it. That works fine. The other is supposed to take that date and add 6 days to it. For some reason that part refuses to work. Am I simply formatting it wrong? I always get 01-06-1970 in my database.
Here is the variable that gets today's date and adds 1 month (works fine)
$renewdate = date('Y-m-d', strtotime('+1 month'));
Here is the variable that adds 6 days to $renewdate (does not work)
$latedate = date('Y-m-d', strtotime('+6 days',$renewdate));
Second argument of strtotime is Unix timestamp. Currently $renewdate is a string. So:
$latedate = date('Y-m-d', strtotime('+6 days', strtotime($renewdate)));
PHP 5.2.0 brought DateTime, why are you still sticking to old functions? OOP approach is better!
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', '2016-08-02 16:16:02');
$dtCreate->add(new DateInterval('P6D'));
This will add 6 days to your DateTime object, see DateInterval for details.
After you added an interval, you may format your object however you wish:
$dtCreate->format('Y-m-d H:i:s');
This will return 2016-08-08 16:16:02, as you can see, it's 6 days later.

How to set date to nth day of this month when using Carbon of PHP?

From the document page of Carbon library, it is stated that it is possible to use simple words to initialize a Carbon object. For example, Carbon::parse('first day of December 2008') or new Carbon('first day of December 2008')
However, when I am trying to set the date to 10th of this month by the following trials
Carbon::parse('10th day of this month')
Carbon::parse('tenth day of this month')
Carbon::parse('10th of this month')
Carbon::parse('tenth of this month')
Carbon::parse('10 of this month')
Carbon::parse('ten of this month')
All of these fails.
Currently I am able to create my desired Carbon object by Carbon::parse('first day of this month')->addDays(9), but the readability is not good. More code is needed to be read and it is very easy to mistaken as 9th of this month at the first glaze.
So, is there a way I can create the Carbon object with one parse only?
It's a bit ugly, but this should work
Carbon::parse('10th ' . date('M'));

Calculate total subscription end date from multiple subscription end dates in PHP

Today is 2014-11-16.
I got these three dates in the future but I want them to be only one date instead.
2014-12-15 21:27:12
2014-12-15 21:32:20
2014-12-16 12:22:09
I want to get the total end date from today. That would be aproximately three month into the future but how can i calculate it to be the same date each time from the three dates above?
How can this be achieved with PHP's DateTime and DateInterval classes?
This might help:
$dateTimeObject=new DateTime(date('Y-m-d h:i:s', strtotime('2014-12-16 12:22:09')));
//to get diff with now
$diff = $dateTimeObject->diff(new DateTime(date('Y-m-d h:i:s')));
Then,
//to get diff months
echo $diff->m;
//to get diff hours
echo $diff->h;
//to get diff minutes
echo $diff->i;
And so on.
It is highly recommended to take a look at the PHP's DateTime official document.
Note that, DateTime is available on PHP >=5.2.0

this week in PHP 5.3

I try to find an answer but without result
the problem is:
in sunday function return date for Monday next week
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Moscow'));
$date->setTimestamp(strtotime('Monday this week'));
echo $date->format("d.m.Y");
but in other days (except Sunday) its return correct value of Monday.
I ever set locale manualy , which has a monday - the first day of week, but PHP "think" the Sunday is still firs day. is it bug ?? or i do some wrong ?
There seem to be known idiosyncrasies with strtotime. It's mentioned in the comments:
http://www.php.net/manual/en/datetime.formats.relative.php#108317
They don't mention different locales specifically but it would be extremely unsurprising if strtotime() does not correctly behave for next/this week under different locales.
Try this Monday or next Monday and see if one of those gets what you want.

Easiest way to get the unix time stamp for the date exactly 1 month back in php

I want to get the unix time stamp for that day exactly 30days back from current day.
What is the best method?
Can i use this to get the date 30 days back, is this the best method?
$day = date('Y-m-d', strtotime('-30 days'));
a google search brings me to mktime() function in php. But how do i combine both and get the unix time stamp for the day? What is the easiest and fastest method?
You just need to use the strtotime("-1 month"); function. That will return a UNIX timestamp.
$date = date_create();
date_sub($date, date_interval_create_from_date_string('1 m'));
echo date_format($date, 'U');
How to get previous month and year relative to today, using strtotime and date? watch this question. there is the discussion about the funny strtotime("-1 month"); bug.

Categories