PHP: strtotime() gives wrong output - php

In my script, I have a given end date. To get the start date, I subtract 23 months to the end date. Basically, what my script should do is to output 24 months (w/ year) - the last month/year to be printed should always be the specified end date.
For some reason, my script isn't returning my desired results. Given the $end = '2013-07-05', the script returns the result correctly. It prints out Aug 11 to Jul 13 which is correct.
But for some dates (e.g. $end = '2013-07-31'), the output is wrong. The result should be Sep 11 to Aug 13. But in this case, it outputs Aug 11 to Aug 13 which is absolutely wrong.
Here's my code:
<?php
$end = strtotime('2013-07-31 +1 month');
$date = strtotime('2013-07-31 -23 month');
$start = $month = $date;
$months = "";
while($month < $end)
{
$months .= date('M y', intval($month))." ";
$month = strtotime("+1 month", intval($month));
}
echo $months;
?>
I think there's something wrong with strtotime(). Thanks in advance.

You can't really use month calculations like that, especially when dealing with end-of-month values:
e.g. if it's July 31, what's -1 month to strtotime?
php > echo date('r', strtotime('2013-07-31 -1 month'));
Mon, 01 Jul 2013 00:00:00 -0600
A human would probably pick out June 30th, but strtotime isn't human. This DOES work for February 28th and generally any date where the day value is <= 28. Once you get into the 29,30,31 area, then you get these unexepected results
php > echo date('r', strtotime('2013-04-28 -1 month'));
Thu, 28 Mar 2013 00:00:00 -0600

How about
$endMonth = '8';
$year = '2013';
$i = 24;
while( $i > 0 ){
$month = ($endMonth - $i)%12;
if( $month == 0 ){
$year = $year - 1;
$month = 12;
}
$months .= date('M y', strtotime($year.'-'.$month.'-02'));
$i--;
}

Based on Marc B's answer I modified the script to deal with the 29,30,31 of each month. What I did was, if the date is 29, 30, or 31, it will be subtracted with 3 days so that the date will be either 28 or below and would work just fine with the current code that I have. It worked for me so I guess I'll just stick with this for now. Here's the updated code:
<?php
$dt = "2013-07-31";
$dy = strtotime($dt);
$day = date("d", $dy);
if (($day == 29) || ($day == 30) || ($day == 31)){
$dt = strtotime("$dt -3 days");
$dt = date('Y-m-d', $dt);
}
$end = strtotime("$dt +1 month");
$date = strtotime("$dt -23 month");
$start = $month = $date;
$months = "";
while($month < $end)
{
$months .= date('M y', intval($month))." ";
$month = strtotime("+1 month", intval($month));
}
echo $months;
?>
Thanks for your help and insights. :)

Related

Receiving extra 1 in january month calender

function getDates($year){
$dates = array();
for($i = 1; $i <= 366; $i++){
$month = date('m', mktime(0,0,0,1,$i,$year)); // outputs month 01 for jan etc
$wk = date('W', mktime(0,0,0,1,$i,$year)); // this outputs 01 if 1st week etc
$wkDay = date('D', mktime(0,0,0,1,$i,$year)); //weekday eg mon ,sun etc
echo $day = date('d', mktime(0,0,0,1,$i,$year)); // outputs day eg 01,13,23 etc
$dates[$month][$wk][$wkDay] = $day; // storing date in array
}
return $dates;
}
$dates = getDates(2014);
echo '<br/>'.$dates['01']['01']['Wed']; // getting 01
echo '<br/>'.$dates['01']['01']['Thu']; // 01 (should get 02 as it is 2nd jan thu in 2014)
echo '<br/>'.$dates['01']['01']['Fri']; // 03
when i echo $day in for loop i m receiving 01 02 03 04 etc which is correct but when i echo same in $dates array above i am receiving 01 for both 1st month 1st week wednesday and 1st month 1st week thursday. Why? Where Am i wrong? All other dates i am receiving for 2014 calender is correct.
#ymas is right. Because you are using 366 days in 2014, number 366 is the first day in 2015. This results that the first day of 2015 overwrite the value in your current array. Fix this by check the number of days in the year and use that value instead of the static 366. That will be something like this:
function getDates($year){
$dates = array();
$daysInYear = date("z", mktime(0,0,0,12,31,$year)) + 1;
for($i = 1; $i <= $daysInYear; $i++){
$month = date('m', mktime(0,0,0,1,$i,$year)); // outputs month 01 for jan etc
$wk = date('W', mktime(0,0,0,1,$i,$year)); // this outputs 01 if 1st week etc
$wkDay = date('D', mktime(0,0,0,1,$i,$year)); //weekday eg mon ,sun etc
echo $day = date('d', mktime(0,0,0,1,$i,$year)); // outputs day eg 01,13,23 etc
$dates[$month][$wk][$wkDay] = $day; // storing date in array
}
return $dates;
}
Note that you should not use 365 as static value because then 31 dec will not be available on leap year.
I would recommend using the DateTime object for this:
function newGetDates($year) {
$start = new DateTime("$year-01-01");
$end = new DateTime("$year-12-31 23:59");
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
$dates = array();
foreach($period as $day) {
$dates[$day->format('m')][$day->format('W')][$day->format('D')]
= $day->format('d');
}
return $dates;
}
You are generating same timestamp 4x in the loop, which is overkill. Store timestamp in a variable, and then call date() function with that variable: see how.
Even better, faster and less memory consumption solution would be to use DateTime, like this:
function getDates($year) {
$dt = new DateTime("$year-01-01");
while ($year == $dt->format('Y')) {
$dates[$dt->format('m')][$dt->format('W')][$dt->format('D')] = $dt->format('d');
$dt->modify('+1 day');
}
return $dates;
}
demo

PHP display all dates between a set of dates as list

Suppose I have 2 dates say 29 Aug 2014 and 3 Sep 2014. I need to display all dates between these to dates in the below format.
Aug 2014
29 Fri
30 Sat
31 Sun
Sept 2014
01 Mon
02 Tue
03 Wed
I know how to print all the dates like 29,30,31,1,2,3. But what I am unable to do is to get the month names in between.
Pretty easy question to be honest, pretty basic sollution possible..
$dateRange = new DatePeriod(
new DateTime('2014-07-28'),
new DateInterval('P1D'),
new DateTime('2014-08-04 00:00:01')
);
$month = null;
foreach ($dateRange as $date)
{
$currentMonth = $date->format('m Y');
if ($currentMonth != $month)
{
$month = $date->format('m Y');
echo $date->format('F Y').'<br />';
}
echo $date->format('d D').'<br />';
}
Above sollution results in:
July 2014
28 Mon
29 Tue
30 Wed
31 Thu
August 2014
01 Fri
02 Sat
03 Sun
Do mind it needs PHP >= 5.3 (due to the use of DatePeriod), but the actual logic to solve your question is easy to implement regardless of the used PHP version.
$timeS = strtotime("29 Aug 2014");
$timeE = strtotime("3 Sep 2014");
$monthS = -1;
$time = $timeS;
while ($time < $timeE) {
if ($monthS != date("n", $time)) {
echo date("M Y", $time) . "\n";
$monthS = date("n", $time);
}
echo date("d D", $time) . "\n";
$time = strtotime("+1 day", $time);
}
Edit: After doing it I'm pretty ok with #hindmost comment :)
I think , this is the complete code , as you wanted.
Executed code is here...
http://phpfiddle.org/main/code/3cbe-4855
<?php
$currentMonth = null;
$timeS = strtotime("29 Aug 2013");
$timeE = strtotime("3 Sep 2014");
$time = $timeS;
while ($time < $timeE) {
$month = date("M", $time);
$year = date("Y", $time);
if ($month != $currentMonth)
echo "<br /><h3>".$month."- ".$year."</h3>";
$currentMonth = $month;
echo "<br />".date("d D", $time);
$time = strtotime("+1 day", $time);
}
?>

php - for loop for each month of year

I want a loop that checks the current month, 12 months in the future and 4 months in the past.
For example: Today is 1st August 08. My loop should go through April, May, June, July, August, September, October, November, December, January, February, March, April, May, June, July, and August.
I have tried strotime but I don't know how I can loop 4 months back and 12 months in the future.
Here is my code
$i = 1;
$month = strtotime('2013-08-01');
while($i <= 12) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
I think Yoshi was almost there with his answer, but using DatePeriod with DateTime is more consistent and makes for more readable code IMHO:-
$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);
foreach($period as $date){
//$date is an instance of \DateTime. I'm just var_dumping it for illustration
var_dump($date);
}
See it working
This can be quite tricky, here's how I would do it:
$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo
// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2
for ($i = 0; $i < 16; $i += 1) {
$cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
$month_name = date('F Y', strtotime($cur_month . " months"));
var_dump(month_name);
}
something like this?:
$start = -4;
$end = 12;
for($i=$start; $i<=$end;$i++) {
$month_name = date('F Y', strtotime("$i months"));
echo $month_name;
echo "<br>";
}
Your code, just slightly modified.
date_default_timezone_set('UTC');
$i = 1;
$month = strtotime('-4 month');
while($i <= 16) {
$month_name = date('F', $month);
echo $month_name;
echo "<br>";
$month = strtotime('+1 month', $month);
$i++;
}
Simplest solution:
for($i=-4; $i<=12; $i++) {
echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
}
Explanation:
The loop starts at -4 and goes all the way upto 12 (total 17, including 0). The ternary statement inside strtotime() simply checks if $i is positive, and if it is, a + is inserted so that we'll get the results for strtotime("+1 months") and similar.
Ta-da!
Using DateTime is the easiest and more readable way.
I would do it like this:
$from = new DateTime('-4 month');
$to = new DateTime('+12 month');
while($from < $to){
echo $from->modify('+1 month')->format('F');
}

How to retrieve an array of each date within two epochs

I have two epochs. I want to figure out all the dates that are valid within the two epochs.
For example, if I have the epochs 946684800 (Sat, 01 Jan 2000 00:00:00 GMT) and 947203200 (Fri, 07 Jan 2000 00:00:00 GMT), I want to be able to get: 01/01/2000, 02/01/2000, 03/01/2000, 04/01/2000, etc.
If you have PHP 5.3 or newer, you could do this:
$date1 = new DateTime;
$date1->setTimestamp(946684800);
$date2 = new DateTime;
$date2->setTimestamp(947203200);
$interval = new DateInterval('P1D');
while ( $date1 <= $date2 )
{
$dates_in_between[] = $date1->getTimestamp();
$date1->add($interval);
}
Alternatively, you could use this:
// 1 day = 60 seconds * 60 minutes * 24 hours = 86400
for ($date = 946684800; $date <= 947203200; $date += 86400)
$dates_in_beteween[] = $date;
$dates_in_between will contain a list of "dates" in between.
PHP time values are just Unix timestamps - seconds since Jan 1/1970. Going off PHP 5's datetime object:
$start = strtotime('01 Jan 2000');
$end = strtotime('07 Jan 2000');
for ($d = $start; $d <= $end; $d += 86400) { // increment by 86,400 seconds, aka 1 day
echo date('d/m/Y', $d);
}
There's better ways of going about it, using the DateTime / DateInterval objects, but this is just to show the basics.
Given that your epoch is in seconds you could always add the number of seconds found in a day to the first epoch:
946684800 + 86400 = 946771200 -> Sun, 02 Jan 2000 00:00:00 GMT
And go on like this, I explain better:
947203200 - 946684800 = 518400 / 86400 = 6 (exactly 6 days)
so (PSEUDOCODE):
for(int i = 946684800; i<946684800 ;i+=86400){
day = getDate(i);
}
$epoch1 = '946684800';
$epoch2 = '947203200';
$i = 0;
while($time < $epoch2) {
$time = mktime(0, 0, 0, date("m", $epoch1) , date("d", $epoch1)+$i, date("Y",$epoch1));
echo date('d/m/Y', $time)."<br>";
$i++;
}
If understanding the question right, you want every DAY within the 2 epochs (2000-01-01 and 2000-01-07)..
Can be done like so:
<?php
$epoch1 = 946684800;
$epoch2 = 947203200;
$difference = $epoch1 - $epoch2;
..
//count days
$amountOfDays = round(($epoch2-$epoch1)/86400);
//looping all days
for($i=1; $i<=$amountOfDays; $i++) {
echo date('d/m/Y', $epoch1+($i*86400);
}
?>
$start = strtotime('2011-06-01');
$end = strtotime('2011-06-15');
$date = $start;
$anArray = array();
while ($date <= $end) {
$date = strtotime("+1 DAY", $date);
$anArray[] = $date;
}

Get timestamps of current week

I have a DateTime of current day. I need to get two unix timestamps of beggining and ending of current week. How can I use dateperiod or dateinterval class?
$now = time();
$beginning_of_week = strtotime('last Monday', $now); // Gives you the time at the BEGINNING of the week
$end_of_week = strtotime('next Sunday', $now) + 86400; // Gives you the time at the END of the last day of the week
if (date('w', time()) == 1)
$beginning_of_week = strtotime('Today',time());
else
$beginning_of_week = strtotime('last Monday',time());
if (date('w', time()) == 7)
$end_of_week = strtotime('Today', time()) + 86400;
else
$end_of_week = strtotime('next Sunday', time()) + 86400;
public static function getDaysInWeek($timestamp)
{
$monday = idate('w', $timestamp) == 1 ? $timestamp : strtotime("last Monday", $timestamp);
$days = array();
for ($i = 0; $i < 7; ++$i)
{
$days[$i] = strtotime('+' . $i . ' days', $monday);
}
return $days;
}
The simplest way I can think of is this (I'm assuming the usual European week format, replace with other day names to your liking):
$when = new DateTimeImmutable('1974-08-21 22:30:00'); // Wednesday
echo $when->modify('monday this week')->format('r'), PHP_EOL;
echo $when->modify('monday next week -1 second')->format('r'), PHP_EOL;
Mon, 19 Aug 1974 00:00:00 +0200
Sun, 25 Aug 1974 23:59:59 +0200
I've used DateTimeImmutable for simplicity, you can also use regular DateTime and clone objects.
Edge cases (date is either Monday or Sunday) should work as expected:
echo $when->modify('wednesday this week')->format('r'), PHP_EOL;
echo $when->modify('wednesday this week +1 day -1 second')->format('r'), PHP_EOL;
Wed, 21 Aug 1974 00:00:00 +0200
Wed, 21 Aug 1974 23:59:59 +0200

Categories