Format date as the first/last day of the month [duplicate] - php

This question already has answers here:
In PHP, is there an easy way to get the first and last date of a month?
(12 answers)
Closed 9 years ago.
I have date strings in "Y-m" format. Day is irrelevant here. Now I need to create another date from these date strings wheere day should be either 1st or the last day of the month. I have tried this code:
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-d');
The problem here is that it creates dates with the current day of the months. How can I force it to do it for the 1st or last day?
Thanks

first day:
$dt = $curDate->format('Y-m-01');
last day:
$dt = $curDate->format('Y-m-t');
t: Number of days in the given month
http://php.net/manual/en/function.date.php

You can specify hard code value
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-1'); //if you want first day
$dt = $curDate->format('Y-m-t'); //if you want last day

Related

Creating an array of dates with a sequence of intervals between days [duplicate]

This question already has answers here:
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
(13 answers)
Closed 1 year ago.
I am trying to populate an array of dates between a provided start and end date using the PHP Carbon library. This would be straight forward if the dates did not have a particular sequence..
Here is the scenario:
I need to populate the dates array with four days in each week. For example the sequence of these dates must like so with Tuesday being the day of the start date:
Tuesday, Thursday, Saturday, Sunday
So I need a way of getting the start date and adding 2 days then 2 days then 1 day before iterating to the next week.
Is this possible to do using Carbon (CarbonPeriod/CarbonInterval)?
Or would my solution for this need to be a custom implementation?
Carbon is perfectly able to do this:
// $startDate as mentioned should be a valid Carbon date pointed to Tuesday
$dates = [];
for ($currentDate = $startDate; $currentDate <= $endDate; ) {
$dates[] = $currentDate;
$currentDate->addDays(2);
$dates[] = $currentDate;
$currentDate->addDays(2);
$dates[] = $currentDate;
$currentDate->addDay();
}
Why not simply use DateTime::add in a loop (adding 2 days, then 2 days, then 1 day)?
Here is the documentation

How to increment Month (like Jan, Feb) In php [duplicate]

This question already has answers here:
increment date by one month
(21 answers)
Closed 3 years ago.
Let's say I have a date in the following format: Jun-2019 (Month-Year)
With PHP, I want to increment the date by one month, and I want the year to be automatically incremented, if necessary (i.e. increment from December 2019 to January 2020).
Regards.
You can use \DateInterval for increment \DateTime and then format \DateTime to string:
$date = \DateTime::createFromFormat('M-Y', 'Jun-2019');
$interval = new \DateInterval('P1M');
$date->add($interval);
$dateString = $date->format('M-Y');

Add one month to gmdate using current format [duplicate]

This question already has answers here:
Add a 1-hour expiration to the current time/date
(5 answers)
Closed 7 years ago.
Hello I have the following php code:
$date = gmdate("Y-m-d\TH:i:s\Z"); /*Format: '2015-05-24T00:00:00Z'*/
How would I simply add 1 month to the above date while keeping the same format?
I tried a few things that lead to nowhere, I am sure there should be a way of doing it that I can't seem to find.
Any help is much appreciated!
Thanks,
Al
try below:
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime("+1 month"));
Try with -
$date = gmdate("Y-m-d\TH:i:s\Z", strtotime('+ 1 MONTH'));
echo $date;
It will add one month to the date.

How can I get the last day of the month's timestamp? [duplicate]

This question already has answers here:
Timestamps of start and end of month
(7 answers)
Closed 8 years ago.
I have the current unix timestamp stored in variable $current_time. Using this variable, I want to find out exactly what the timestamp is for the very last day of the current month.
How can I do this in PHP 5.4+?
Here's a working example:
// this is if your $current_time is not within the current month (although the code below is valid for every single month and every single timestamp)
$month = date("M", $current_time);
$last_day_timestamp = strtotime('last day of ' . $month);
echo date("d-m-Y", $last_day_timestamp);
// if your $current_time is within the current month
$last_day_timestamp_of_this_month = strtotime('last day of this month')
Also you could use this as a reference for other relative date/time formats:
http://php.net/manual/en/datetime.formats.relative.php

Get the following day after a month [duplicate]

This question already has answers here:
PHP strtotime +1 month adding an extra month [duplicate]
(7 answers)
How can I get a date after 15 days/1 month in PHP?
(6 answers)
Closed 9 years ago.
Just a noob question,
For example I have date 2013-08-01 // 1 August 2013
How do I get same day next month. ex. 2013-09-01 // 1 Sept 2013
Thanks
I'm not a PHP dev, but it seems to me you want DateTime::add
$date->add(new DateInterval('P1M'));
As noted in the documentation though, you need to be careful near the end of a month - adding 1 month to January 31st 2001 ends up with March 3rd, for example. (Not the behaviour I'd choose, but that's a different matter.) If you're always dealing with the 1st of the month, you should be okay.
With using strtotime
echo date("Y-m-d",strtotime('next month',strtotime('2013-08-01')));
You can use the date_add function (procedural) or Datetime::add (OO). Here is an example of each:
$date = new DateTime('2000-01-01');
$date->add(new DateInterval::createFromDateString('1 month'));
or
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 month'));
Try this:
$dateTest = date('Y-m-d', strtotime("$dateTest + 1 month"));

Categories