strtotime issue - php

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'))?>

Related

Missing minutes in strtotime() / timestamp

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

How to format date in PHP from a string of concatenated numbers?

I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.

PHP: How to get current time in hour:minute:second?

In order to get the date in the right format I want I used date("d-m-Y"). Now I want to get the time in addition to the date in the following format H:M:S How can I procede ?
Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language's capabilities.
Looking up a function is easy, just visit http://php.net/<function name> and it will forward you to the appropriate place. For the date function, we'll visit http://php.net/date.
We immediately learn a couple things about this function by examining its signature:
string date ( string $format [, int $timestamp = time() ] )
First, it returns a string. That's what the first string in the above code means. Secondly, the first parameter is expected to be a string containing the format. There is an optional second parameter for passing in your own timestamp (to construct strings from some time other than now).
date("d-m-Y") // produces something like 03-12-2012
In this code, d represents the day of the month (with a leading 0 is necessary). m represents the month, again with a leading zero if necessary. And Y represents the full 4-digit year. All of these are documented in the aforementioned link.
To satisfy your request of getting the hours, minutes, and seconds, we need to give a quick look at the documentation to see which characters represents those particular units of time. When we do that, we find the following:
h 12-hour format of an hour with leading zeros 01 through 12
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
With this in mind, we can no create a new format string:
date("d-m-Y h:i:s"); // produces something like 03-12-2012 03:29:13
Hope this is helpful, and I hope you find the documentation has benefiting to your development as I have to mine.
You can combine both in the same date function call
date("d-m-Y H:i:s");
You can have both formats as an argument to the function date():
date("d-m-Y H:i:s")
Check the manual for more info : http://php.net/manual/en/function.date.php
As pointed out by #ThomasVdBerge to display minutes you need the 'i' character

strtotime of today

Hallo, I want to find the difference between today and another date,
convert todays date into unix time format here
<?php
echo '1st one'.strtotime(date("d.m.Y")).'<br>';
echo '2nd one'.strtotime(date("m.d.Y")).'<br>';
?>
The first echo is producing some value, but not the second one. What is the bug in it...please help..
strtotime makes assumptions based on the date format you give it. For instance
date("Y-m-d", strtotime(date("d.m.Y"))) //=> "2010-09-27"
date("Y-m-d", strtotime(date("m.d.Y"))) //=> "1969-12-31"
Note that when given an invalid date, strtotime defaults to the timestamp for 1969-12-31 19:00:00, so when you end up with an unexpected date in 1969, you know you're working with an invalid date.
Because strtotime is looking for day.month.year when you use . as the delimiter, so it sees "9.27.2010" as the 9th day of the 27th month, which obviously doesn't exist.
However, if you change it to use / as the delimiter:
date("Y-m-d", strtotime(date("d/m/Y"))) //=> "1969-12-31"
date("Y-m-d", strtotime(date("m/d/Y"))) //=> "2010-09-27"
In this case, strtotime expects dates in month/day/year format.
If you want to be safe, Y-m-d is generally a good format to use.
It's worth pointing out that strtotime() does accept words like "today" as valid input, so you don't need to put a call to date() in there if all you want is today's date. You could just use strtotime('today');.
Come to think of it, a simple call to time(); will get you the current time stamp too.
But to actually answer the question, you need to consider that d.m.Y and m.d.Y are ambiguous - if the day of the month is less than the 12th, it is impossible to tell which of those two date formats was intended. Therefore PHP only accepts one of them (I believe it uses m/d/Y if you have slashes, but for dots or dashes it assumes d-m-Y.
If you're using strtotime() internally for converting date formats, etc, there is almost certainly a better way to do it. But if you really need to do this, then use 'Y-m-d' format, because it's much more universally reliable.
On the other hand, if you're accepting date input from your users and assuming that strtotime() will deal with anything thrown at it, then sadly you're wrong; strtotime() has some quite big limitations, of which you've found one. But there are a number of others. If you plan to use strtotime() for this sort of thing then you need to do additional processing as well. There may also be better options such as using a front-end Javascript date control to make it easier for your users without having to rely on strtotime() to work out what they meant.
strtotime does not consider 09.27.2010 to be a valid date...
You could check it like this:
<?php
// will return false (as defined by the docs)
echo var_dump(strtotime("09.27.2010"));
?>
The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp. US time format is : MM DD YYYY
look here for the Information about which formats are valid http://www.php.net/manual/en/datetime.formats.php. But what do you mean with deference between 2 dates? You mean the Timespan between 2 dates?
echo (time() - strotime("- 2 days")) . " seconds difference";
Something like that?
strtotime would not take the d.m.y format. good way is Y-m-d

Wrong output from date()

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"

Categories