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
Related
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 a date in oW format. This is the 4 digit year in ISO 8601 format followed by the week number. As an example, 201301 represents the week starting on Monday, Dec. 31, 2012.
How can I convert this back to a timestamp. Going from timestamp to string works with date('oW',$ts). How do I go backwards? I am interested in getting the Monday of the week represented in oW format. It would be very silly if PHP provided a way to go in one direction ut did not bother with the inverse.
ISO 8601 expects week dates in the following format:
YYYY\WWW
For example:
2012W52
is the start of the 52st week in 2012 what is actually the 2012/12/24.
To convert week dates back into a timestamp you can use the php function strtotime(). The code will look like follows:
$oW = '2012W53';
$time = strtotime($oW);
// will output: 2012-12-31T00:00:00+01:00 (I'm in CEST)
echo date('c', $time);
However the format you posted above - 201300 - cannot being understood by strtotime(). There are two problems :
00 is not allowed for the week. Allowed values for week are from 01 to 53
You missed the 'W' in the middle. But it belongs to the ISO 8601 standard.
In my tests I further figured out, that 2012W53 points to the 2012/12/31 not 2013W00 (or W01 as you may think)
Further you may read this comment in the php manual and the Wikipedia article on this.
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
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"