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'))
);
Related
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:
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'))
);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to find first day of the next month and remain days to this date with php
I have this to get 1st day of current month
$_SERVER['dataInicio'] = date('Y') . "-" . date('m') . "-" . "1";
I need something similar to get 1st day of NEXT month
$date = new DateTime('now');
$date->modify('first day of next month');
echo $date->format('D') . '\n';
$date->modify('first day of this month');
echo $date->format('D') . '\n';
This seems like a duplicate or similar post to this for the next month
but for the next month something like this if you want to use strtotime
date("m/l/Y", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));
this will return
12/Saturday/2012 if you want just the days name just set the date format to l (lower case L)
date("l", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));
How to find first day of the next month and remaining days till this date with PHP
or this
In PHP, is there an easy way to get the first and last date of a month?
$_SERVER['dataInicio'] = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));
Should do what you're wanting.
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get first day of week in PHP?
Given a timestamp I need to find the first day the week the timestamp belongs to.
e.g.
$format = "first day of week";
//convert to time
$time = strtotime("2011-07-01 00:00:00");
//format the time to the first day of the week
$date = strtotime($format,$time);
//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);
The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.
Any ideas would be much appreciated thanks,
$time = strtotime("2011-07-01 00:00:00");
$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
Test it :
$time=[Your Request Time];
$dayofweek = date("w",strtotime($time));
if( $dayofweek != 0 )
$firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );
else
$firstdayOfWeek = strtotime($time);