0

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?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    Check the documentation for `mktime`: http://www.php.net/manual/en/function.mktime.php. Use `strtotime` instead. – Aziz Saleh Jun 03 '14 at 16:07

3 Answers3

2

Use this one:

date("M-d-Y h:i:s", strtotime($user_birthday));
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Bipul Khan
  • 697
  • 1
  • 5
  • 10
1

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • yes thanks for it. would me mind answering the question that i commented on stephen. – user3703850 Jun 03 '14 at 16:15
  • @user3703850 I have added `echo date("M-d-Y h:ia", $db_user_birthday);` to the answer now. This will properly format the output of `strtotime` so it is human readable. – Giacomo1968 Jun 03 '14 at 16:21
0

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
Community
  • 1
  • 1
S. Dixon
  • 842
  • 1
  • 12
  • 26
  • God I feel so dumb...anyway thanks... is there any format of strtotime as user is going to set the value...i need to display they should enter in this format and if they don't enter required format show them error – user3703850 Jun 03 '14 at 16:13
  • @user3703850 Your question makes no sense in the context of the code you are providing. – Giacomo1968 Jun 03 '14 at 16:17
  • @user3703850 I know what you are saying. Piggybacking endless questions is not well received here. So you might want to improve your reputation so you can ask real questions instead of doing this. – Giacomo1968 Jun 03 '14 at 16:21