php - Strange Behaviour In date() inside For Loop - php

I am trying to show month shorten name using date() inside for loop. But it doesn't want to return all of the month names, instead of just the first month name that is Jan.
for($d=1; $d<=12; $d++){
if($d < 10) {
$s = 0 . $d;
echo date("M", $s) . '<br/>';
}
}
It will return:
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
Jan
I don't know why the value of variable $d won't increment if it is located inside the date() function.
Can someone here explain me why it is happened like that?
Best Regards

for($d=1; $d<=12; $d++){
$myDate = mktime(1,1,1,$d,1,2014);
echo date("M", $myDate) . '<br/>';
}
This has been tested. Output:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
...
Basically, the second parameter of the date() function needs to be a timestamp (in seconds).
The mktime() function will create a timestamp based on the hours/minutes/seconds/months/days/years that you provide.
http://uk1.php.net/mktime

Date() requires timestamp as second parameter, not "month number" as you provide. So, for seconds 1-12 from Unix epoch, month will be January.

If you want to print the next 12 months from current date this will help you.
<?php
for ($m=1; $m<=12; $m++) {
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
echo $month. '<br>';
}
?>
What you are doing is simply looping current day 12 times and printing the same dates month in each iteration. Thats why you are getting the same month.

for ($m=1; $m<=12; $m++) {
$month = date('M', strtotime("2014-$m-01"));
echo $month. '<br>';
}

Related

strtotime wrong value after I added 1 month

this line gives me the correct value:
echo strtotime(date('d.m.Y 00:00:00'));
Output:
1623362400
Converted:
Fri Jun 11 2021 00:00:00 GMT+0200 (CEST)
Now I would like to get the same, but +1 month.
I tried this:
echo strtotime('+1 month', date('d.m.Y 00:00:00'));
Output:
2678411
Converted:
Sun Feb 01 1970 01:00:11 GMT+0100 (CET)
Where is my fault?
You're not using well the first strtotime() param. Passing 2 arguments ask the function to "compare" and use the second param as a baseTimestamp.
For the result you want, you only need the first param, and have to specify you want "+1 month" before. You can do it concatenating the string with date() result :
echo strtotime(date('d.m.Y 00:00:00').' +1 month');
You don't need to nest string operations or convert back and forth:
$t = strtotime('midnight +1 month');
echo date('r', $t);

Add 30 days to date [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code

strftime giving wrong month name (February only...)

I have the following code, to get the month name of a certain date, and then using to get the name of the month in Hebrew.
$thismonthname = date("F", mktime(0, 0, 0, $thismonthnumber, 10));
date_default_timezone_set('Asia/Tel_Aviv'); // Set timezone to Israel
$locale = 'he_IL.utf8'; setlocale(LC_ALL, $locale); // Set Locale to Hebrew
$thismonthnameheb = strftime('%B', strtotime($thismonthname));
It works perfectly, except for February.
When I print out $thismonthname it says "February" but when I print out $thismonthnameheb it says מרץ (march) in Hebrew.
Going crazy, I can't figure this out.
You're doing too much conversion from time to string and back.
Instead of converting time -> string -> time, simply keep the time value and base the results on that:
$thismonthtime = mktime(0, 0, 0, $thismonthnumber, 10);
$thismonthname = date("F", $thismonthtime);
date_default_timezone_set('Asia/Tel_Aviv'); // Set timezone to Israel
$locale = 'he_IL.utf8'; setlocale(LC_ALL, $locale); // Set Locale to Hebrew
$thismonthnameheb = strftime('%B', $thismonthtime);
This has nothing to do with Hebrew, since, at least on the particular version of PHP I am using (5.3.10),
echo strftime('%d %B', strtotime('February'));
gives
01 March
As suggested in the comments, this arguably unexpected behaviour is due, basically, to PHP's assumption that the day of a given month is the 30th unless the user actually specified a different value. Hence for February we overflow to the 1st of March.
A look at this reference might prove useful.
Note the sequence:
php > $time = mktime(0,0,0,2, 10);
php > echo date('r', $time);
Mon, 10 Feb 2014 00:00:00 -0600 // everything ok here, I'm in UTC-6
php > date_default_timezone_set('Asia/Tel_Aviv');
php > echo date('r', $time);
Mon, 10 Feb 2014 08:00:00 +0200 // note how it's now 8 hours "later"
php > $month = date('F', $time);
php > echo $month;
February
php > $newtime = strtotime($month);
php > echo date('r', $newtime);
Sat, 01 Mar 2014 00:00:00 +0200 // hey! it's march again!
You screwed up by stripping your date down to a simple month name, then expecting PHP to magically be able to guess what you're expecting to do. If you feed it just a month, it's free to pick whatever it wants for the year/day/time values.

Project Euler #19 code seems right. What am I missing?

Problem 19:
You are given the following information, but you may prefer to do some
research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September, April, June and
November.
All the rest have thirty-one, Saving February alone, Which
has twenty-eight, rain or shine. And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a
century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
I thought that using PHP for this would be a breeze since it has so many built-in time and date functions. My code is really quite simple, so I'm having a hard time seeing what I'm doing that's wrong.
My code:
<?php
echo "<pre>";
$sunday_count = 0;
for( $year = 1901; $year <= 2000; $year++ ) {
for( $month = 1; $month <= 12; $month++ ) {
// Produces a date in format: 1/1/2000
$date = $month . "/1/" . $year;
$time = strtotime( $date );
$is_sunday = ( date( 'l', $time ) == "Sunday" );
echo "$date "
. ( $is_sunday ? 'was a Sunday. ' : '' )
. "<br>";
if( $is_sunday ) $sunday_count++;
}
}
echo "Answer: $sunday_count";
echo "</pre>";
?>
The solution my code comes up with is 169, which is not correct. Any idea?
Edit 1
The solution is supposed to be 171.
Using Wolfram Alpha and my Windows clock, I've doubled checked several of the Sundays which my code reports. All of them check out OK.
So it seems my code is reporting valid and legitimate Sundays, but somehow it has missed two of them.
Edit 2
I made the following minor change to the formatting of the date in my code:
$date = sprintf('%d-%02d-01', $year, $month); // formats yyyy-mm-dd
I then used #MadaraUchiha's code to generate an array containing the 171 correct dates.
After comparing his dates with mine, these are the two missed dates:
1901-09-01
1901-12-01
Edit 3
Codepad also shows that these dates are not Sundays (but they really should be).
And I am certain that the dates are correctly being interpreted as YYYY-MM-DD because one of the dates my code offers to the solution is 2000-10-01, which would only be a Sunday if 10 is the month, not the day.
Edit 4
So apparently if on a 32 bit system, Unix timestamps won't work outside of the range:
Fri, 13 Dec 1901 20:45:54 GMT
to
Tue, 19 Jan 2038 03:14:07 GMT
The reason it might not work on some systems using timestamps is that the range of a Unix timestamp on 32-bit systems is from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT, so you miss almost all months in the first year.
64-bit systems have larger integers which makes the range bigger (in PHP).
I get 171 with this (much shorter, and more readable) code, using DateTime objects:
header("Content-type: text/plain"); //Browser display
$time = new DateTime("1901-01-01");
$end = new DateTime("2000-12-31");
$counter = 0;
while (!$time->diff($end)->invert) { //$time isn't greater than $end
$time->modify("+1 month");
if ($time->format("l") == "Sunday") {
$counter++;
echo $time->format("Y-m-d") . " was a Sunday!\n";
}
}
echo "\nTotal number of Sundays: $counter";
When using DateTime objects, you're treating dates as Dates and not as numbers or strings. This gives you a huge flexibility advantage over any other approaches.
The problem on this code is the line
$date = $month . "/1/" . $year;
this should be
$date = "1/".$month."/" . $year;
You mixed the places of months and days.

Convert month number to month short name

I have a variable with the following value
$month = 201002;
the first 4 numbers represent the year, and the last 2 numbers represent the month. I need to get the last 2 numbers in the month string name eg. Feb
My code looks like this
<?php echo date('M',substr($month,4,6)); ?>
I can I go about to obtain the month name
Append "01" and strtotime will be able to parse the string :
echo date('M', strtotime($month . '01'));
The second parameter of date is a timestamp.
Use mktime to create one.
$month = 201002;
$monthNr = substr($month, -2, 2);
$timestamp = mktime(0, 0, 0, $monthNr, 1);
$monthName = date('M', $timestamp );
This may help you..
$month = substr($month, -2, 2);
echo date('M', strtotime(date('Y-'. $month .'-d')));
You can use the DateTime Class to get a Date data structure using date string and format. Then get a date string with any format like this:
$month = 201002;
$date = DateTime::createFromFormat('Yd', $month);
$monthName = $date->format('M'); // will get Month name
$mydate = "201002";
date('M', mktime(0, 0, 0, substr($mydate, 4, 2), 1, 2000));
Being a programmer, and even knowing nothing of PHP data magic, I'd made it
$month = intval(substr($input_date,4,2));
$mons = explode(" ","Zer Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
echo $mons[$month];

Categories