I have an action which accepts a year and an optional month. I need to query all articles written within that time, so I need the absolute first and last valid datetime for that timespan.
$start = new \DateTime();
$start->setDate((int) $year, (int) ($month ?: 1), 1);
$start->setTime(0, 0);
$end = clone $start;
$end->add(new \DateInterval($month ? 'P1M' : 'P1Y'));
$end->sub(new \DateInterval('PT1S'));
Is there a cleaner way to write this?
$start = mktime(0, 0, 0, $month ?: 1, 1, $year);
$end = mktime(23, 59, 59, $month ?: 12, $month ? date('t', $start) : 31, $year);
or
$start = new \DateTime("$year-" . ($month ?: 1) . '-1 00:00:00');
$end = new \DateTime("$year-" . ($month ?: 12) . '-' . ($month ? $start->format('t') : 31) . ' 23:59:59');
Not sure if you'd consider this cleaner, but it's shorter and does without relative calculations.
The cleanest and most understandable way to write this is probably:
if ($month) {
$start = new \DateTime("$year-$month-1 00:00:00");
$end = new \DateTime("$year-$month-" . $start->format('t') . ' 23:59:59');
} else {
$start = new \DateTime("$year-1-1 00:00:00");
$end = new \DateTime("$year-12-31 23:59:59");
}
You might not need to involve DateTime much at all here if you're going to use this as value for an SQL query.
Related
I need to convert any date to a date within 1 month from today. Dates that are smaller than today should be converted to next month.
For example, current day is 2018-09-11 and dates are:
$dates = ['2018-08-01', '2018-09-11', '2018-09-15', '2018-10-15', '2018-12-31', '2019-01-01', '2019-01-31'];
Results should be as follows, respectively:
$result = ['2018-10-01', '2018-09-11', '2018-09-15', '2018-09-15', '2018-09-30', '2018-10-01, '2018-09-30'];
Edit: I have tried so far using strtotime('+1 month') but it fails on dates such as 2018-10-31 since it'll give 2018-12-01. It also fails on using strtotime('-1 month') on dates such as 2018-10-31 where it gives 2018-10-01 instead of 2018-09-30.
My current solution is following, but I believe it can be done MUCH simpler somehow:
$timestamp = strtotime('2018-10-31');
if (date('d', $timestamp) > date('t')) {
$date = date('Y-m-t');
} elseif(date('d', $timestamp) < date('d')) {
$year = date('Y');
$month = date('m') == 12 ? 1 : (intval(date('n')) + 1);
if ($month < 10) {
$month = '0'.$month;
}
$day = date('d', $timestamp);
$date = $year . '-' . $month . '-' . $day;
} else {
$date = date('Y-m') . '-' . date('d', $timestamp);
}
You can use
if('2018-09-11'<date("Y-m-d"))
date("Y-m-d", strtotime("+1 month", '2018-09-11'));
for example
I am Using Bellow Script. but it give me Result but not According To My Requirement.
This is the Week number i need. Week Calendar
$month = "10";
$year = "2016";
$beg = (int) date('W', strtotime("first monday of $year-$month"));
$end = (int) date('W', strtotime("last monday of $year-$month"));
print(join(', ', range($beg, $end)));
above code Gives Result. 40, 41, 42, 43, 44
With PHP >= 5.3 do
$month = "10";
$year = "2016";
$beg = (int) date('W', strtotime("first day of $year-$month"));
$end = (int) date('W', strtotime("last day of $year-$month"));
print(join(', ', range($beg, $end)));
How can one get the timestamps of the first and last minutes of any month using PHP?
You can use mktime and date:
$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 59, date("n"), date("t"));
That is for the current month. If you want to have it for any month, you have change the month and day parameter accordingly.
If you want to generate it for every month, you can just loop:
$times = array();
for($month = 1; $month <= 12; $month++) {
$first_minute = mktime(0, 0, 0, $month, 1);
$last_minute = mktime(23, 59, 59, $month, date('t', $first_minute));
$times[$month] = array($first_minute, $last_minute);
}
DEMO
With PHP 5.3, you can do
$oFirst = new DateTime('first day of this month');
$oLast = new DateTime('last day of this month');
$oLast->setTime(23, 59, 59);
In PHP 5.2
Note: as AllThecode pointed out in the comments below, this next example only works if you do the $oFirst portion first. If you add +1 month to new DateTime the result will jump an extra month ahead on the last day of the month (as of php 5.5.9).
$oToday = new DateTime();
$iTime = mktime(0, 0, 0, $oToday->format('m'), 1, $oToday->format('Y'));
$oFirst = new DateTime(date('r', $iTime));
$oLast = clone $oFirst;
$oLast->modify('+1 month');
$oLast->modify('-1 day');
$oLast->setTime(23, 59, 59);
Use mktime for generating timestamps from hour/month/day/... values and cal_days_in_month to get the number of days in a month:
$month = 1; $year = 2011;
$firstMinute = mktime(0, 0, 0, $month, 1, $year);
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$lastMinute = mktime(23, 59, 0, $month, $days, $year);
I think better then
$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 0, date("n"), date("t"));
is:
$first_minute = mktime(0, 0, 0, date("n"), 1);
$last_minute = mktime(23, 59, 0, date("n") + 1, 0);
This requires PHP > 5.2 and need adjustement for the "minutes" part
$year = ...; // this is your year
$month = ...; // this is your month
$month = ($month < 10 ? '0' . $month : $month);
$start = new DateTime($year . '-' . $month . '-01 00:00:00');
$end = $start->modify('+1 month -1 day -1 minute'); //perhaps this need 3 "->modify"
echo $start->format('U');
echo $end->format('U');
(not tested)
Ref: http://www.php.net/manual/en/class.datetime.php
$date = new \DateTime('now');//Current time
$date->modify("-1 month");//get last month
$startDate = $date->format('Y-m-01');
$endDate = $date->format('Y-m-t');
Best Way do like this..
$first_day = date('m-01-Y h:i:s',strtotime("-1 months"));
$last_day = date('m-t-Y h:i:s',strtotime("-1 months"));
Let's assume I have two dates in variables, like
$date1 = "2009-09-01";
$date2 = "2010-05-01";
I need to get the count of months between $date2 and $date1($date2 >= $date1). I.e. i need to get 8.
Is there a way to get it by using date function, or I have to explode my strings and do required calculations?
Thanks.
For PHP >= 5.3
$d1 = new DateTime("2009-09-01");
$d2 = new DateTime("2010-05-01");
var_dump($d1->diff($d2)->m); // int(4)
var_dump($d1->diff($d2)->m + ($d1->diff($d2)->y*12)); // int(8)
DateTime::diff returns a DateInterval object
If you don't run with PHP 5.3 or higher, I guess you'll have to use unix timestamps :
$d1 = "2009-09-01";
$d2 = "2010-05-01";
echo (int)abs((strtotime($d1) - strtotime($d2))/(60*60*24*30)); // 8
But it's not very precise (there isn't always 30 days per month).
Last thing : if those dates come from your database, then use your DBMS to do this job, not PHP.
Edit: This code should be more precise if you can't use DateTime::diff or your RDBMS :
$d1 = strtotime("2009-09-01");
$d2 = strtotime("2010-05-01");
$min_date = min($d1, $d2);
$max_date = max($d1, $d2);
$i = 0;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
$i++;
}
echo $i; // 8
Or, if you want the procedural style:
$date1 = new DateTime("2009-09-01");
$date2 = new DateTime("2010-05-01");
$interval = date_diff($date1, $date2);
echo $interval->m + ($interval->y * 12) . ' months';
UPDATE: Added the bit of code to account for the years.
Or a simple calculation would give :
$numberOfMonths = abs((date('Y', $endDate) - date('Y', $startDate))*12 + (date('m', $endDate) - date('m', $startDate)))+1;
Accurate and works in all cases.
This is another way to get the number of months between two dates:
// Set dates
$dateIni = '2014-07-01';
$dateFin = '2016-07-01';
// Get year and month of initial date (From)
$yearIni = date("Y", strtotime($dateIni));
$monthIni = date("m", strtotime($dateIni));
// Get year an month of finish date (To)
$yearFin = date("Y", strtotime($dateFin));
$monthFin = date("m", strtotime($dateFin));
// Checking if both dates are some year
if ($yearIni == $yearFin) {
$numberOfMonths = ($monthFin-$monthIni) + 1;
} else {
$numberOfMonths = ((($yearFin - $yearIni) * 12) - $monthIni) + 1 + $monthFin;
}
I use this:
$d1 = new DateTime("2009-09-01");
$d2 = new DateTime("2010-09-01");
$months = 0;
$d1->add(new \DateInterval('P1M'));
while ($d1 <= $d2){
$months ++;
$d1->add(new \DateInterval('P1M'));
}
print_r($months);
Using DateTime, this will give you a more accurate solution for any amount of months:
$d1 = new DateTime("2011-05-14");
$d2 = new DateTime();
$d3 = $d1->diff($d2);
$d4 = ($d3->y*12)+$d3->m;
echo $d4;
You would still need to handle the leftover days $d3->d if your real world problem is not as simple and cut and dry as the original question where both dates are on the first of the month.
This is a simple method I wrote in my class to count the number of months involved into two given dates :
public function nb_mois($date1, $date2)
{
$begin = new DateTime( $date1 );
$end = new DateTime( $date2 );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
$counter = 0;
foreach($period as $dt) {
$counter++;
}
return $counter;
}
In case the dates are part of a resultset from a mySQL query, it is much easier to use the TIMESTAMPDIFF function for your date calculations and you can specify return units eg. Select TIMESTAMPDIFF(MONTH, start_date, end_date)months_diff from table_name
strtotime is not very precise, it makes an approximate count, it does not take into account the actual days of the month.
it's better to bring the dates to a day that is always present in every month.
$date1 = "2009-09-01";
$date2 = "2010-05-01";
$d1 = mktime(0, 0, 1, date('m', strtotime($date1)), 1, date('Y', strtotime($date1)));
$d2 = mktime(0, 0, 1, date('m', strtotime($date2)), 1, date('Y', strtotime($date2)));
$total_month = 0;
while (($d1 = strtotime("+1 MONTH", $d1)) <= $d2) {
$total_month++;
}
echo $total_month;
I have used this and works in all conditions
$fiscal_year = mysql_fetch_row(mysql_query("SELECT begin,end,closed FROM fiscal_year WHERE id = '2'"));
$date1 = $fiscal_year['begin'];
$date2 = $fiscal_year['end'];
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$te=date('m',$ts2-$ts1);
echo $te;
How to get the UNIX timestamp range of the current hour, i mean one of the first second and the other for the last second. so if it's 18:45 i would get the timestamp of 18:00 and 18:59.
Thanks
You can get the components of the current time with getdate(), and use mktime() to find the timestamps:
$date = getdate();
$start = mktime($date['hours'], 0, 0);
$end = $start + (60*60);
You can also use date(), which is slightly simpler:
$start = mktime(date("H"), 0, 0);
$end = $start + (60*60);
$start = mktime(date('H'), 0, 0);
$end = mktime(date('H'), 59, 59);
Could be generalized for any timestamp, not just the current time, as:
$time = time(); // some timestamp
$start = mktime(date('H', $time), 0, 0, date('n', $time), date('j', $time), date('Y', $time));
$end = mktime(date('H', $time), 59, 59, date('n', $time), date('j', $time), date('Y', $time));