How to get last Monday from a specific date - php

I am trying to get the date of Monday from a specific date.
I try to use
strtotime("last monday")
But it's giving me current last Monday date not from a specific date.
Like I want to know the last Monday date on 2020-10-11 which is 2020-10-05

You can use the class DateTime to create an object at the desired date, and then modify it :
$date = new DateTime('2020-10-11');
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05
As pointed jspit in comments, if the current date is monday and then if this date should be returned, a simple check can be added to avoid returning the wrong date :
$date = new DateTime('2020-10-11');
if ($date->format('N') != 1) // If the date isn't already monday
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05

Related

How to say "next month 15th" in PHP's DateTime?

What I want to do:
Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like
$day = new DateTime('15th of next month');
but it's not possible to set a fixed day by it's number.
Can anybody help ?
EDIT: I've changed the DateTime from this month to next month to make the problem more clear.
You can use DateTime::createFromFormat(). If you only pass the day value, it'll default to the current month and year.
$date = DateTime::createFromFormat('d', '15');
echo $date->format('Y-m-d'); // 2018-10-15
Edit for the new question requirements:
$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); // 2018-11-15

'Round' up a date to the beginning of the next month

I need to take an existing date from a variable and display a date that is always the 1st day (01) of whatever the next month is (and accounting for the year as well).
So if I have this:
$date = '2017-03-17'; // YYYY-MM-DD
I need to take that date and make it output this:
2017-04-01 // The first day of the next month
Just another example...
$date = '2017-12-23'; // YYYY-MM-DD
...should be converted to...
2018-01-01 // The first day of the next month
You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');
Simply increment the date by 1 month and set the date to 1st of the month. Do -
date('Y-m-01', strtotime('+1 MONTH', strtotime($date)));
Working code
you can get it using
$date = '2017-03-17';
echo date('Y-m-01', strtotime('+1 month',strtotime($date)));
https://eval.in/790237

Get first day date of the week on a given date PHP

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

DateTime modify string for first day of current year

im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:
<?php
$time = new DateTime();
// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";
// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";
>
I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.
You should specify the year too, as you can see in this example:
"first day of January 2008"
from the official doc.
Update: It works on php version >= 5.3.6
On v5.5.6
echo date('Y-m-d', strtotime('first day of January this year'));
Result: 2013-01-01
To get the day of the week for the first of the year
or the first day of the month
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
Result Wed For May with D
Result Wednesday with l
This work for me (PHP 5.6 - not tested on older version)... as we talk for DateTime object
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');

Week number and Week day

I have week number of current year and week day generated by date() like this.
$week_number = date('W');
$week_day = date('w');
I need to format this. How can I get starting date of this week? Or day of month with $week_number and $week_day?
Update:
Maybe this article helps you. It describes how to get the start and end date of a given week.
<?php
// Monday
echo date(
datetime::ISO8601,
strtotime("2006W37"));
// Sunday
echo date(
datetime::ISO8601,
strtotime("2006W377"));
?>
Where the format is <year>W<week-number><daynumber> and Monday is 1.
Update 2:
Maybe another possibility is to use strtotime() this way:
echo strtotime("last Monday");
echo strtotime("next Sunday");
You can combine this with date() to get the date in the desired format.
You can get the day of the month with date('d') directly.
date() documentation.

Categories