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)
Related
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);
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') ) );
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')
This question already has answers here:
How can I find the first and last date in a month using PHP?
(12 answers)
Closed 9 years ago.
I want to know how to get LAST DAY when I just know month and year. The month is integer and year is integer too. This is the codes :
<?php
$month = 2;
$year = 15; //I don't know why after "date('y',strtotime($tanggal_awal))" I get 15 not 2015
//how to know last date ????
?>
The real source is not like this. It's very long. I want to know PROPER MANNER TO GET last day WHERE MONTH IS FEBRUARY AND YEAR IS 2015.
You can use the cal_days_in_month() function, the total number of days is the same as what the last day will be.
You may need to install the calendar functions in PHP, follow instructions here: http://www.php.net/manual/en/calendar.installation.php
As suggested in the comments you can also use date('t'), which doesn't require the above extension to be installed.
use this to get the number of days of the desired month...
int cal_days_in_month ( int $calendar , int $month , int $year )
Which gives the last date of the month.
cal_days_in_month
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).