I can get this week Thursday by doing below
$startdate = (new DateTime())->setTime(0,0,0);
$startdate->setISODate($startdate->format("Y"), $startdate->format("W"), 4);
But if i try a relative date it get this week Thursday before or on Thursday and gets next Thursday after Thursday.
$startdate = (new DateTime('thursday'))->setTime(0,0,0);
I also tried "this thursday" relative date but it doesnt return what i would excpect, it acts like "thursday". I can use the setISODate but im am just curious if i can do with a relative date since it would be easier.
http://php.net/manual/en/datetime.formats.relative.php
Try this:
$startdate1 = (new DateTime('thursday last week'))->setTime(0,0,0); //2017-01-19
$startdate2 = (new DateTime('thursday this week'))->setTime(0,0,0); //2017-01-26
$startdate3 = (new DateTime('thursday next week'))->setTime(0,0,0); //2017-02-02
Related
I want to display the first day of the month in 'day of the week' format.
For example, The below code should select the month of August and say the day of the week 01 falls on which is Thursday, but for some reason, the below code outputs Friday which is wrong, on some months such as May it correctly says Wednesday.
$monthNumber = 3;
$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
$dateTest = date("Ym" . 1, strtotime($monthNumber . " month", $base));
$unixTimestamp = strtotime($dateTest, $base);
echo date("l", $unixTimestamp);
Does anyone have any ideas to make it show the correct day?
$base fixes a bug with it not showing the correct month.
And why won't you use the dateTime?
echo (new \DateTime('first day of august 2019'))->format('l');
Read more about Relative formats.
You have to use date() at first:
$base = date('Y-m',time()) . '-01 00:00:00';
And dont have to set the first second :)
To get the day of the week falling on the first day of any month you can use the prefix first day of e.g.
echo (new \DateTime('first day of August'))->format('l');
This will show correctly Thursday for August 2019.
If you want to do it dynamically using numbers for months, you can create a dummy date like you were trying to do.
$timeString = sprintf('01.%02d.%04d', 8, 2019); // replace the numbers with your variables
echo (new \DateTime($timeString))->format('l');
Am trying to get the timestamp of previous day midnight
That is if today is thursday , get the timestamp of wednesday midnight.
How do i go about this
i have tried
$lastdaymidnight = strtotime() //am stuck
What do i need to add to strtotime?
try this
$date = new DateTime();
$date->setTimestamp(strtotime('yesterday midnight'));
One liner:
$date = new DateTime('yesterday midnight');
I have Carbon date variable.
Carbon::parse("2018-08-01") //tuesday
I want to add days until next monday ("2018-08-07").
Is there command like
Carbon->addDaysUntil("monday"); ->addMonthUntil("september")
and so on.
So i want to change current date to begining of next week, month, year
Old question but there's a nice way of doing this at the moment.
$date = Carbon::parse('2018-08-01')->next('Monday');
Additionally, if you want to check if your date is monday first, you could do something like this:
$date = Carbon::parse(...);
// If $date is Monday, return $date. Otherwise, add days until next Monday.
$date = $date->is('Monday') ? $date : $date->next('Monday');
Or using the Carbon constants as suggested by #smknstd in the comment below:
$date = Carbon::parse(...);
// If $date is Monday, return $date. Otherwise, add days until next Monday.
$date = $date->is(Carbon::MONDAY) ? $date : $date->next(Carbon::MONDAY);
What you can do is determine the current date, get the start of the week (Monday) and add a week to get the next week.
$date = Carbon::create(2017, 8, 30);
$monday = $date->startOfWeek();
$mondayOneWeekLater = $date->addWeeks(1); // $date->addWeek();
Rinse and repeat for months and years but as Maritim suggests it's in the docs. ;-)
Source: http://carbon.nesbot.com/docs/
I have a date 2015-12-16
i want to get the date of the first day's date for the current week of my date
here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)
how could i do it ?
thank you
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.
Try this:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
I am trying to get the second thursday from a datetime.
Code
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
The above code returns 2013-08-15 10:00 instead of 2013-08-08 10:00
But it works correctly when I use the following code
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second wednesday'.$test->format('H:i'))->format('Y-m-d H:i');
First day of month of 2013-August is Thursday.
And there is bug in the code that if if the first day is Thursday , first Thursday will come in the next week i.e 8-August and like wise 2nd second Thursday will come on 15-August
this code will help
echo date('Y-m-d', strtotime('second thursday of august 2013'));
updated code
$test = new DateTime('2013-08-02 10:00');
echo date('Y-m-d', strtotime("second thursday of august 2013")).' '.$test->format('H:i');
Added By RC
From PHP strtotime manual page
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given
weekday in a month where that weekday was the first day of the month
would incorrectly add one week to the returned timestamp. This has
been corrected in 5.2.7 and later versions.
try this,
echo date('Y-m-d', strtotime('second thursday of august 2013'));
try this one. i have already check and it shows the result you want.
$test = new DateTime('2013-08-00 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday'.$test->format('H:i'))->format('Y-m-d H:i');
'second Thursday' means the second Thursday after the current date. So the expected outcome is 2013-08-15. If you want the second Thursday of the given month, you could specify it like this:
$test = new DateTime('2013-08-02 10:00');
echo $test->modify('second thursday of this month '.$test->format('H:i'))->format('Y-m-d H:i');
Note that 'this month' refers to the date currently stored in the DateTime object, not on the date the code is executed.