PHP DateTime - Return specific month [duplicate] - php

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

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)

calculating calendar week php [duplicate]

This question already has an answer here:
Week number and Week day
(1 answer)
Closed 9 years ago.
If anyone has an idea how can I calculate a calendar week depending on given date in php, for example 06.06.2013 (format like this 2013-06-06). is calendar week 23. I was searching for the solution all morning and found nothing useful. Any help or link, anything would be appreciated. Thank you
Use date("W")
echo date("W", strtotime('2013-06-06'));
See it in action
date('W') should give you the week of the year. RTM
If you don't have your time as a unix timestamp you can use strtotime() first and pass it as the second parameter
ex. date('W',strtotime($my_time_string))
You can see the PHP manual here
Like this:
$time = '2013-06-06';
$calendar_week = date('W', strtotime($time));

PHP get the date of this weeks monday [duplicate]

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).

PHP check if 1 day passed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
What I want to do is to get day from database, current date. And check if difference between them > 1 day:
$curdate= date("Y-m-d H:i:s");
$dbdate is value stored in datetime format in db.
$dif=$curdate-dbdate;
How to check if $dif>1 day ??
Assuming the stored date is expressed in the same time zone as the server, you can convert it to a timestamp using strtotime, and compare it to strtotime("-1 day"):
if (strtotime($dbdate) < strtotime("-1 day"))
frobnicate();
You can get just the day from each date.
$day = intval($curdate= date("d"));
This will get the day as an in. Do the same for the time of the data base and you get two integer representing the day. Using that you can calculate how many days have pass.
Beware that the last line should look like this:
$dif = abs($curdate-$dbdate);

Categories