PHP get the date of this weeks monday [duplicate] - php

This question already has answers here:
Get first day of week in PHP?
(39 answers)
Closed 9 years ago.
Any way to take a date like this 2013-05-29 in PHP and get the date (in the same format) of that weeks monday? So the output would be like this: 2013-05-27

date('Y-m-d', strtotime('last sunday +1 day', strtotime('2013-05-29')));
last sunday +1 day because last monday would return the Monday of the previous week if $timestamp actually was a Monday already.
And parsing of the original date value 2013-05-29 in a second step because all together as one argument does not work well (mixing of absolute and relative date values is something strtotime does not like very much).

Related

Get next 2 days not counting current day in PHP [duplicate]

This question already has answers here:
PHP Strtotime without current time?
(2 answers)
Closed 4 months ago.
I have an Unix timestamp like this 1660293621 (2022-08-12 8:40). I want to get next 2 days not counting current date. I expect the result to be 2022-08-15 00:00.
I tried
strtotime("+3 Days", $current_date)
but it returns 2022-08-15 8:40, not 00:00
How can I get that in PHP? Thank you~
$Today=date('y:m:d');
// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));
Reference:
Increase days to php current Date()
I figured it out, just add 0:00 will help
$next2days = strtotime("+3 days 0:00", $current_date);

How to get the day of the week in DateTime? [duplicate]

This question already has answers here:
How to find the date of a day of the week from a date using PHP?
(9 answers)
Closed 3 years ago.
How to get the day of the week in DateTime ?
I can’t find the way to get the day of the week from a DateTime.
It’s can be number or string, both are ok.
$today = new DateTime("today");
echo $ today->//whatever to get Friday, Monday or 5, 0
How to get the day of the week in DateTime ?
Edit:
I am using DateTime, not date.
You can use format() function
$today->format('l') //Sunday through Saturday
$today->format('w') //0 (for Sunday) through 6 (for Saturday)

PHP DateTime - Return specific month [duplicate]

This question already has answers here:
PHP date; How to find the next year?
(9 answers)
Closed 5 years ago.
Trying to understand the DateTime class, but am having problems figuring out how to return the date for the first day of a future month relative to now.
Essentially, I want to return (for example), the first day of next August. If I use:
new DateTime( date('Y-m-d', strtotime('first day of August') ) )
it returns 2017-08-01, which has already passed. I wanted to return 2018-08-01. Using "first day of next August" doesn't work. Is there a way to accomplish what I want using the strtotime parser?
new DateTime( date('Y-m-d', strtotime('first day of August +1 year') ) );

PHP CreateFromFromFormat() Month & Year Only [duplicate]

This question already has an answer here:
date_create_from_format() returns wrong value. [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to create data from format using Datetime Class as following
$date = DateTime::createFromFormat('m-Y', '02-2016');
echo $date->format('Y-m-d');
Out put 2016-03-01, i was expecting to get 2016-02-01
is it a bug? or i am understating this function in a wrong way?
If you provide month and year, then PHP will provide a default day, using the current day of the current month (ie 30) today
That gives an effective 2016-02-30, which isn't a valid date..... but PHP allows these types of values where days is higher/lower than days in month, months are higher/lower than months in the year, and just increments/decrements to a valid date..... in this case, 1 additional day after the last day in month 2 or 2016 (29th February 2016) to give 1st March 2016
EDIT
Reference for day/month overflow/underflow behaviour
Note:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).
It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".
If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar).
It's an known bug... or a documented feature, depending on how you look at it. See Mark Baker's answer for more details.
Workaround :
To get the date you expect, just add 01 to your input as a value for the day of the month.
$input = '02-2016';
$date = DateTime::createFromFormat('d-m-Y', '01-' . $input);
echo $date->format('Y-m-d');
// OUTPUT : 2016-02-01

PHP - Last Saturday at 6pm [duplicate]

This question already has answers here:
PHP Date Function Seven days previous
(3 answers)
Closed 8 years ago.
I have a query where I want to pass in two variables and I feel like I am almost there but need a bit of guidance.
I have a cron job set up to run at 6pm every Saturday that will select tickets for the last week.
Select tickets that are between 6pm last saturday and 5:59 today bearing in mind that this willl be run at 6pm every saturday.
I'm looking for help to create two variables.
6pm last Saturday
5:59pm today
Have looked at strtotime and date but can't see anything that would fit?
Check strtotime:
$time = strtotime("last Saturday 6 PM");
echo date("Y-m-d H:i:s", $time);
A mysql solution could be:
SELECT *
FROM tickets
WHERE
ticket_datetime BETWEEN
DATE_SUB(CONCAT(DATE(NOW()),' 06:00:00'), INTERVAL 1 WEEK)
AND
CONCAT(DATE(NOW()),' 06:00:00')

Categories