How to get last day of the month using php [duplicate] - php

This question already has answers here:
How to find the last day of the month from date?
(30 answers)
Closed 9 years ago.
How to get last day of the month using php ?
<?php
echo date('?-?-?');
?>

<?php
$day=new DateTime('last day of this month');
echo $day->format('M jS');
?>

The date function documentation says that t represents number of days in current month:
$day = date( 't-m-Y' );

try this
$day=date('Y-m-t'); // gives last day of current month
OR
$d = new DateTime( '2013-05-03' );
echo $d->format( 'Y-m-t' );

Try:
$last_day = date('t-m-Y');
where t means the last date of the current month.
PHP: date - Manual
How can I find the first and last date in a month using PHP?

Using the function date you would do
$day = date("t");
Please read the documentation

Try this
$date = new DateTime();
$lastDayOfMonth = $date->modify(
sprintf('+%d days', $date->format('t') - $date->format('j'))
);

Related

Current month Minus 1 month [duplicate]

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.

php get oldest record and add 1 week [duplicate]

This question already has answers here:
php date format YYYY-MM-DD minus or add one week from now?
(4 answers)
Closed 9 years ago.
I'm looking at making an automated post once a week blog so I can create more postings and then have them go out once a week. I'm having some trouble getting the greatest time and adding 1 week to it.
My database I'm using "datetime".
So the string is in "2013-03-20 09:42:41".
I can get the value of greatest post blog_date, but how do I add 1 week to the string?
date('$blog_date', strtotime("+1 week"));
Thanks for your time ^^
ANSWER WORKS:
$blog_date = date('Y-m-d h:i:s', strtotime("+1 week", strtotime($newest)));
You can use the Datetime object to add a week easily
http://php.net/manual/en/book.datetime.php
$date = new DateTime('2013-03-20 09:42:41');
$date->modify('+1 week');
Try this..
$blog_date = "2013-03-20 09:42:41";
$date2 = strtotime(date("Y-m-d", strtotime($blog_date)) . "+1 week");
echo date('Y-m-d', $date2);
Output
2013-03-27

Get the last day of the month? [duplicate]

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

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

Given a time, how can I find the time one month ago

Given a time, how can I find the time one month ago.
strtotime( '-1 month', $timestamp );
http://php.net/manual/en/function.strtotime.php
In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime
We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.
// say its "2015-11-17 03:27:22"
$dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
Hope this adds some more info for this question.
<?php
$date = new DateTime("18-July-2008 16:30:30");
echo $date->format("d-m-Y H:i:s").'<br />';
date_sub($date, new DateInterval("P1M"));
echo '<br />'.$date->format("d-m-Y").' : 1 Month';
?>
PHP 5.2=<
$date = new DateTime(); // Return Datetime object for current time
$date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
echo $date->format('Y-m-d'); // To set the format
Ref: http://php.net/manual/en/datetime.modify.php
This code is for getting 1 month before not 30 days
$date = "2016-03-31";
$days = date("t", strtotime($date));
echo date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months.
I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.
I do something like this:
$current_ym = date('ym',strtotime("-15 days",$ts));

Categories