This question already has answers here:
PHP subtract 1 month from date formatted with date ('m-Y')
(11 answers)
Closed 5 years ago.
How do i properly minus 1 month for the current month ?
$current_month1 = date('m');
$current_month = $current_month1-1;
echo $current_month;
//current ouput
6
//desired output
06
check the following:
$now = new \DateTime("now");
$past = $now->modify("-1 month");
DateTime::modify docs
Also you can do it using DateInterval, the docs has example.
You can use date in combination with strtotime for this.
echo date('m', strtotime('last month')); // 06
The m operator in date will get you:
echo date('m', strtotime('now - 1 month'));
Gives 06.
Related
This question already has answers here:
get last day of the next month in php?
(5 answers)
Closed 1 year ago.
Today - 2021-03-31
<?php
echo date("Y-m-t H:i:s", strtotime("+1 month"));
?>
Expected result
2021-04-30
Result
2021-05-31
Adding a month adds a certain number of days, so it throws things off. The easiest way is to get the first day of next month, then use t to format it to the total number of days:
echo date("Y-m-t H:i:s", strtotime("first day of next month")); // 2021-04-30 10:27:35
Should be simple as below.
<?php
$month_end = strtotime('last day of next month', time());
echo date('D, M jS Y', $month_end).'<br/>';
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is there a way to use DateTime::CreateFromFormat to convert a 0-6 integer into the name of a day of the week?
I am creating a calendar for a script and would like to let users customize the way the days of the week display (Mon vs Monday) so using an array of the week day names is not desirable.
How can I use CreateFromFormat to do this?
Step 1:
Get timestamp of first day of week +X (our integer) days.
// assuming monday = 0, sunday = 6
$day = 2; // our integer (wednesday)
// get timestamp of most recent monday +X days
$ts = (date('N') == 1) ? strtotime("today +$day days") : strtotime("last monday +$day days");
Step 2:
Print the short & long names of the weekday.
echo date('l', $ts); // Wednesday
echo date('D', $ts); // Wed
Hope I understand your question.
$day = 0;
$date = DateTime::createFromFormat('j-M-Y', $day . '-Feb-2010');
echo $date->format('l');
would output :
Sunday
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have this function that runs on the server and I want a different output based on the day of the week, Saturday and Sunday should give weekend message and different for the weekdays.
$timein = date("Y-m-d h:i:s");
$dw = date("D", $timein);
$tm = date("e", $timein);
echo "Current Date: ".$timein."<br>";
echo "Day of the week: ".$dw."<br>";
echo "Timezone: ".$tm."<br>";
and this is the output:
Current Date: 2015-05-15 06:07:12 Day of the week: Wed
Timezone: America/Denver
We are Friday and I was expecting Fri, I was using w instead of D but I always was getting 3 in the results.
You need to use strtotime function to convert it into a Unix timestamp. Just update your code into.
$dw = date("D", strtotime($timein));
$tm = date("e", strtotime($timein));
This question already has answers here:
how to get the first and last days of a given month
(10 answers)
Closed 9 years ago.
How do I get the first day of the month in php? And the first day of one month early.
So as example:
$startdate = 01-05-2014
$enddate = 01-06-2014
The enddate has to be the first day of the current month so if it is 26-01-2014 the enddate is 01-01-2014
and the startdate is then 01-12-13
As soon as you have the dates as a Timestamp, you can use this to get the first day of the month:
date("Y-m-1", $timestamp) ;
You can get the last day of a month, when you use:
date("t", $timestamp) ; // would give you 31 in January
In a complete example for your problem:
$timestamp = strtotime("26-01-2014") ;
$startdate = date("Y-m-1", strtotime("-1 month", $timestamp)) ; // to subtract one month -> use strtotime
$enddate = date("Y-m-1", $timestamp) ;
See also in the documentation of PHP for strtotime and date.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP last day of the month
Is there any function like $date->getMonthDays() or $date->getLastDayOfMonth() in PHP to get the number of days in a given month (or the last day number)?
$start = new DateTime('2012-02-01');
$end = clone $start;
// Interval = last day of the month minus current day in $start
$interval = $start->getLastDayOfMonth() - intval($start->format('j'));
$end->add(new DateInterval('P' . $interval . 'D'));
EDIT: thanks, voted to close, it's a duplicate, sorry for asking...
The php date function gives you the number of days in the month with 't'
date("t");
See: http://php.net/manual/en/function.date.php
It's simple to get last month date
echo date("Y-m-t", strtotime("-1 month") ) ;
echo date("Y-m-1", strtotime("-1 month") ) ;
at March 3 returns
2011-02-28
2011-02-1
t gives you the total number of days in the current month. j gives you the current day of the month.
Using modify and some subtraction from format-ing the datetime, you can get to the end of the month.
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);