PHP mktime notice - php

I want to change given date and time or date only into Unix time.
I tried like this:
mktime("Jan-12-2012 2:12pm");
But it’s not working:
Even in PHP documentation I looked at many examples and many of them don’t consist the matter that I want.
And when I try:
$user_birthday=$_POST["user_birthday"];
$db_user_birthday=empty($user_birthday)?"":mktime($user_birthday);
$_POST["user_birthday"] was given value from form that is jan-12-2012 2:12pm
it show error like this:
Notice: A non well formed numeric value encountered in C:\Program
Files (x86)\Ampps\www\admin\index.php on line 76
How do I fix it or display time into Unix?

Use this one:
date("M-d-Y h:i:s", strtotime($user_birthday));

You should be using strtotime instead of mktime:
Parse about any English textual datetime description into a Unix
timestamp.
So your code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
Then you can process that date like this to get it formatted as you want it to:
echo date("M-d-Y h:ia", $db_user_birthday);
So your full code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
echo date("M-d-Y h:ia", $db_user_birthday);
Note I also added spaces to your code in key points. The code will work without the spaces, but for readability & formatting, you should always opt to use cleaner code like this.

You should take a look at this answer: convert date to unixtime php
Essentially, you have mixed up mktime() with strtotime(). strtotime() allows you to parse an English textual string into a Unix timestamp. mktime() constructs a unix datetime based on integer arguments.
For example (again taken from the question above)
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
echo strtotime("2009-11-03 11:24:00PM");
1257290640

Related

False dateresult using date() php

I want to parse the date 1938 1938+02:00 using date() & strtotime().
My code:
echo date("Y", strtotime("1938+02:00"));
gives me as result "2014"..
What am i doing wrong?
For something like this just get the first four characters of the string. No need to work with dates and such:
echo substr('1938+02:00', 0, 4);
Demo
But if you insist on using date functionality you'll need to use DateTime::createFromFormat() as that date string is not a standard format.
$date = DateTime::createFromFormat('YP', '1938+02:00');
echo $date->format('Y');
Demo
date("Y"); only return the year. That's what the Y does.
See the Manual page for date for other options.
EDIT
Another thing to consider, is that timestamps only go back to 1970. That's what a timestamp is (the number of seconds since 1970).
So, that's going to give you a negative value for the timestamp.
Your date string is not in an acceptable format. here is a list of acceptable formats for strtotime

A non well formed numeric value encountered

I'm working on a project in which I need to match two dates. One stored in the database and one is the current date time. I've decided to use unix timestamp for this. I'll convert these both values to unix timestamp and would then compare.
I used the below code to generate the unixtimestamp, but it gave me the following error :-
A non well formed numeric value encountered
I have gone through half a dozen questions on stack overflow, but none helped my case.
$date =date("Y-m-d H:i:s A");
echo mktime($date);
What might be the solution ?
You are using the wrong function, you don't want to use mktime but strtotime
$date =date("Y-m-d H:i:s A");
echo strtotime($date);
But the format of the date you are passing isn't currently supported by PHP. You'll have to drop A (PM and AM).
To drop the AM/PM:
echo strtotime(substr($date, 0, -2));
If you want to generate the current unix time, just use mktime() with no arguments. Check out http://php.net/manual/en/function.mktime.php

Why won't PHP strtotime work for a date in this format mdy?

I have a problem with verifying dates in PHP 5.2.17. I need the user to be able to enter the six digit date without any punctuation so Jan. 15, 2014 would be 011514. (This is due to input device limitation and for user convenience).
Tested and this is not working:
$testDate = "011514";
$myDate = date('mdy', strtotime($testDate));
echo $myDate;
I would expect that myDate is the same as test date but it's not, it prints out today's date 042314 (April 23, 2014)! PHP doesn't like that there is no punctuation in my original string. If I change $testDate = "01-15-14" then PHP spits out the correct string "011514".
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
Extra info:
PHP version is 5.2.17 so date_create_from_format is NOT able to be used.
There's a limit to what strtotime() can understand. It can't understand everything. That's why they tell us what it does and we, as developers, need to write our code in a way that respects that.
All you need to do in your code is insert slashes and you're all set.
$testDate = "011514";
$date = sprintf('%s/%s/%s',
substr($testDate, 0, 2),
substr($testDate, 2, 2),
substr($testDate, 4, 2)
);
$myDate = date('mdy', strtotime($date));
echo $myDate;
Demo
Answering to your only question
How can I get strtotime() to understand that I want to check dates in 'mdy' format without using date_create_from_format ?
To get strtotime() to understand your format you need to download php sources, patch it so that it supported such a weird format and use your custom build.

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.

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

Categories