I am getting the date from my MySQL database and that date is correct, but when I use the date() function the minute part is stuck at 6 minutes.
MySQL is returning 2010-06-15 09:59:18
Then in PHP I use this code ($row[4] is from my while loop):
date('M d,Y H:m A',strtotime($row[4]))
When I echo this out I get: Jun 15,2010 9:06 AM
I also have tried converting it to a unix timestamp in my SQL query, but it does the same thing. I noticed that the hours work and the seconds work but the minutes are stuck at 6. Does anyone have any ideas on what is going on?
'm' is a representation of month, not minutes using the PHP date() function. So, you are getting the '06', meaning June, not 06 minutes past 9.
It only looks like minutes because of how you formatted your date string but what you are getting there is a numerical representation of the month.
http://us2.php.net/manual/en/function.date.php
m - Numeric representation of a month, with leading zeros
I think the minute arg should be i (lowercase eye), not m.
Use this instead (i instead of m):
date('M d,Y H:i A',strtotime($row[4]))
The capital m is the month, and now it's June ;)
The minute is "i"
Related
I was trying to round down time to a whole minute (as part of bigger rounding mechanism). In my unit tests I figured something strange, this peace of code:
echo date('Y-m-d H:i:00', strtotime('2018-09-31 19:39:45'));
result in:
2018-10-01 19:39:00
What am I doing wrong?
Note: I WAS running this on 2018-10-01
http://sandbox.onlinephpfunctions.com/code/1cb0dd98e9d540616d02ce2d5c00684800af8597
strtotime() doesn't validate dates.
If you do
echo date('Y-m-d H:i:00', strtotime('2018-02-30 19:39:45'));
Outputs
2018-03-02 19:39:00
So, the problem here is that you are using an "invalid" date, and PHP is summing up seconds.
So, the spetember 31 means september 30 + 24 hours (in seconds). When you run date() will get the date in seconds and showing to you the valid date, october first.
Could somebody please take a look into my code:
$godzinaRozpoczecia = "10:35";
$dateStampGodzinaRozpoczecia = strtotime($godzinaRozpoczecia);
echo date("H:m", $dateStampGodzinaRozpoczecia);
RESULT: 10:08
var_dump($godzinaRozpoczecia);
RESULT: string(5) "10:35"
What is wrong? Why do I have minutes missing? In var_dump everything seems to be fine. The same issue occurs when I retrieve the time from the database.
The date() function specifies the following format for "H:m":
H: 24-hour format of an hour with leading zeros
m: Numeric representation of a month, with leading zeros
So you're actually showing the month here, you're looking for i:
i: Minutes with leading zeros
You may have been confused with the format used for the strtotime(), DateTime and date_create() formats. They are different from the date() formats (for some illogical reason).
Try this
date("H:i", $dateStampGodzinaRozpoczecia);
//m is for month.current month is August that is only it gives you 08 as aswer
function DateFormat($dt)
{
return $newDate = date("d/m/Y", strtotime($dt));
}
$cr='2014-02-31';
echo DateFormat($cr);
Input: $cr='2014-02-31';
Output: 03/03/2014
I am passing 2014-02-31 and getting output 03/03/2014.
Please help me out.
PHP's date functions work with dates not strings. And that's an important distinction. Strings are just a bunch of characters in a specified order. Dates have months, days, years, hours, minutes, seconds, timezones, etc. When PHP works with dates it takes all of them into consideration.
So when you pass Feb 31 to a PHP date function it is going to try to make sense of it as a date and not a string. This means it isn't just going to take that date cut it up into bits and then rearrange them as you are expecting. It is going to turn that date into a date representation it can work with and then manipulate it.
As we all know, February does not have 31 days. As a result of the invalid date, PHP is trying to be helpful and taking three days after last day in February of that year (since Feb only has 28 days this year) and giving you that date.
The issue is February most years only has 28 days. 2/31 would be logically translated to 3/3. On a leap year you'd get 3/4...
The strtotime() method as it needs to be very flexible to be able to handle stuff without borking like:
strtotime('2014 February + 31 day - 1 year');
And no I don't think it should error out. When you have a well formed date string, PHP has a deceptively named method called checkdate() you could use:
$crappy_date='2014-02-31';
$date_parts = explode('-', $crappy_date);
$valid = checkdate($date_parts[1], $date_parts[2], $date_parts[0]);
I have created a code like
<?=date('h:m A',strtotime('09:30:00'))?>
I am getting an output like 09:12 AM. The actual result will be 09:30 AM.
Why getting a result like the above?
A quick look at the documentation of the date() function shows the mistake in your code:
m Numeric representation of a month, with leading zeros
i Minutes with leading zeros
So you need i for the minute. The whole format string would be 'h:i A'
However, it would be much better if you didn't use the date function but strftime which uses standardized format variables:
<?=strftime('%I:%M %p', strtotime('09:30:00'))?>
I want to store start time & end time and calculate the total time difference between the two times.Some time i got negative times like -1 min -24 sec by this problem...
if i am echo this code
date('Y-m-d h:m:s',1345810203);
i got the answer as 2012-08-24 02:08:03 ,but i echo this code
strtotime('2012-08-24 02:08:03');
i got the answer as 1345766883 .
If the time will be 1345810167 or 1345810187,give correct time in strtotime & date().
What is the problem ?
You have wrong format.
m is Numeric representation of a month, with leading zeros
i is Minutes with leading zeros
Use following format:
'Y-m-d h:i:s'
Ref: http://php.net/manual/en/function.date.php