Wrong php date('t') - php

I have this script
echo 'giorni mese: '.date('t', $mese_start).' - mese start: '.$mese_start;
output is:
giorni mese: 31 - mese start: 11
But november doesn't have 30 days?
What am I missing?
Update:
right, thank you.

The second argument to date() is a timestamp, which is the number of seconds since 1970-01-01 00:00:00 UTC. The value of $mese_start is 11. So that timestamp is 1970-01-01 00:00:11 UTC, and January has 31 days.
If you want to use $mese_start as a month number rather than a timestamp, you can use mktime() to create a timestamp from a particular date:
$ts = mktime(0, 0, 0, $mese_start);
echo 'giorni mese: '.date('t', $ts).' - mese start: '.$mese_start;

The second argument of date is interpreted as a Unix timestamp, not a month. You are passing "11" which equals some time on January 1st, 1970. And January has 31 days.
You could either construct a valid timestamp for November or use cal_days_in_month:
echo cal_days_in_month(CAL_GREGORIAN, 11, 2018); // 30

Related

phpExcel given timestamp instead of actual date in excel sheet [duplicate]

I want to read dates from an Excel File, but when I print
the dates to the screen I can only see one number instead Of
the date, why?
Excel file:
Result:
Excel treats dates as numbers, with the number representing the number of days after December 31 1899. So day 1 is January 1 1900, and day 43102 is January 3 2018. But wait... your data says January 2 2018! It turns out that Microsoft thinks that 1900 was a leap year and so day 60 is February 29 1900 when in the real world it was actually March 1. Anyway, what that means is that for dates after February 28 1900, you need to subtract one from the day number to get the correct date. So, to convert an Excel day number to a date in PHP, you use the following code:
$dayval = 43102; // you would read from your file here
$date = new DateTime('1899-12-31');
$date->modify("+$dayval day -1 day");
echo $date->format('Y-m-d');
Output:
2018-01-02
Please use this formula to change from Excel date to Unix date, then you can use "gmdate" to get the real date in PHP:
UNIX_DATE = (EXCEL_DATE - 25569) * 86400
and to convert from Unix date to Excel date, use this formula:
EXCEL_DATE = 25569 + (UNIX_DATE / 86400)
After putting this formula into a variable, you can get the real date in PHP using this example:
$UNIX_DATE = ($EXCEL_DATE - 25569) * 86400;
echo gmdate("d-m-Y H:i:s", $UNIX_DATE);
The 86400 is number of seconds in a day = 24 * 60 * 60. The 25569 is the number of days from Jan 1, 1900 to Jan 1, 1970. Excel base date is Jan 1, 1900 and Unix is Jan 1, 1970. UNIX date values are in seconds from Jan 1, 1970 (midnight Dec 31, 1969). So to convert from excel you must subtract the number of days and then convert to seconds

Php unix timestamp with strtotime

I start learn PHP and is very clear I make some error here, I try obtain Unix timestamp of specific hour and minutes of the day:
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime();
$datafmt = $data->format('Y-m-d');
echo strtotime($datafmt,'18:30:00');
?>
The code return 1554951600 and is equal to:
GMT: Thursday, April 11, 2019 3:00:00 AM
Your time zone: Thursday, April 11, 2019 12:00:00 AM GMT-03:00
This is wrong, timestamp should be:
1555018200 is equal to:
GMT: Thursday, April 11, 2019 9:30:00 PM
Your time zone: Thursday, April 11, 2019 6:30:00 PM GMT-03:00
What I doing wrong?
Fixed!
echo strtotime($datafmt. '18:30:00');
, instead . that is my error!
You do not need strtotime() at all. DateTime class is a replacement and is more powerful. Just pass the time to the constructor or set it with the method setTime()
<?php
date_default_timezone_set('America/Argentina/Buenos_Aires');
$data = new DateTime('18:30:00');
// Alternative ways to set the time of the DateTime object
// $data->setTime('18', '30', '00');
// $data->setTime(...explode(':', '18:30:00'));
$datafmt = $data->format('U'); // U means UNIX timestamp
echo $datafmt;

how to php convert this time format?

i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php

Converting from a day of week to unix time in PHP

I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)

PHP - date() reduction bug?

I am using a unix time stamp as the base of my starting date for use in a date ranged query. The start date is not the problem, for the purpose of this example i will use the following time stamp: 1228089600 (01 December 2008 00:00:00).
For use in my query I needed to easily figure out the last day of any given month to the last second so..
date('o-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("o",1228089600)));
This method has been working fine for me for every other month except December.. By taking the start date, adding a whole month (01 January 2009 00:00:00) then taking away 1 second I was expecting to result in the date I required (31 December 2008 23:59:59). However it appears the year is being calculated correctly for the additional month, but not for the subtracting second as the date returned is 31 December 2009 23:59:59.
As I say, this method has worked great until I discovered this problem. But it's a problem I am unable to figure out the cause of or simple solution to..
Any help is greatly appreciated.
This happens because:
flag 'o' - ISO-8601 year number. This has the
same value as Y, except that if the
ISO week number (W) belongs to the
previous or next year, that year is
used instead. (added in PHP 5.1.0)
So in your case happens this:
Date generated is 1st January 2009
Date is decremented with 1 second (so you get 31st December 2008)
But as goes the description above - the week belongs to 2009 so 2009 is returned instead of 2008
(belongs to year means: More days of the week are in 2009 than in 2008 - in the case above: 3 days are in 2008 (mon, tue, wed - 29th,30th,31st) and 4 are in 2009 (thu, fri, sat, sun - 1st,2nd,3rd,4th))
That does indeed seem to be a bug with how mktime() handles a "months" value greater than 12.
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 2, 1, 2008));
2008-01-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 3, 1, 2008));
2008-02-28 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 1, 1, 2008));
2007-12-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 13, 1, 2008));
2009-12-31 23:59:59
php > echo date('o-m-d G:i:s',mktime(0, 0, -1, 13, 1, 2007));
2008-12-31 23:59:59
Your best bet right now is probably to just check the output from date('m', ...)+1 yourself and special-case the calculation if the result is 13.
I did add some exceptions to reduce the year if the month was January, however i also had another date range which is built at the same stage which is used with GA api. I was surprised to see this date range was fine despite the method being similar. The difference being..
Problematic:
date('o-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("o",1228089600)));
Fine:
date('Y-m-d G:i:s',mktime(0, 0, -1, date("m",1228089600)+1, 1, date("Y",1228089600)));
So making the switch appears to have solved the problem.
Hopefully all this may be of use to someone, someday.
Try this:
$start = 1228089600;
$number_of_days_in_month = date('t', $time);
$end = strtotime('+' . $number_of_days_in_month . ' days', $start) - 1;
// subtract one second to get 23:59:59 or don't to get 00:00:00.
// Also note that there can be a leap second.
// $end = $start + $number_of_days_in_month * 86400 - 1; would probably work as well.
echo date('o-m-d G:i:s', $end);

Categories