1

I have two inputs, one for the date in yyyy/mm/dd format and another for time in 12:15 AM. Now I need to save into the the databse a timestamp. I get both inputs lets say:

$my_date = '2013/12/22';
$my_time = '12:50 PM';

How do I get a timestamp to save into db?

Thank you

3 Answers 3

1

Give this a try...

$my_date = '2013/12/22';
$my_time = '12:50 PM';

$full_date = $my_date . ' ' . $my_time;

$timestamp = strtotime($full_date);
Sign up to request clarification or add additional context in comments.

Comments

1

Use DateTime:createFromFormat()

<?php

$my_date = '2013/12/22';
$my_time = '12:50 PM';

$d = sprintf('%s %s', $my_date, $my_time);

$dt = DateTime::createFromFormat('Y/m/d h:i A',  $d);
$ts = $dt->getTimestamp();

var_dump($ts);

Yields:

int 1387716600

Hope this helps :)

Edit

4 Comments

I've upvoted this answer 'cause I believe this is the best and most updated answer so far here, but it seems that somebody after me have downvoted that as well. Just out of curiosity wondering what was the reason for that? Is there anything wrong with this answer?
Thank you @Mahdi. I'd also appreciate a comment on the downvote. Someone downvoted the accepted answer as well. Clearly these answers are not incorrect nor inaccurate. It's pretty mean spirited in my view to downvote without giving some sort of reason/feedback.
Regarding the answer itself, I work with dates and times a lot and I personally prefer to use the DateTime family of classes (DateTimeZone, DateInterval, DatePeriod etc.) because it provides some good functionality and flexibility. That said, there's nothing wrong with strtotime functions either - it's simply a matter of utility and desired functionality. I specifically used createFromFormat in this case because of the unusual format (Y/m/d, is like ISO8601 except it uses slashes) and explicitly defining the input format would minimise possible errors.
Yeah, I'm not sure what's going on with the down voting around here... both of the correct answers (this and mine) were down voted, which doesn't make any sense to me at all.
0

You could use strtotime() to convert it into an UNIX timestamp.

E.g.:

$my_date = strtotime('2013/12/22');
$my_time = strtotime('12:50 PM');

Full date + time timestamp

$timestamp = strtotime('2013/12/22 12:50 PM');

More info: http://php.net/manual/en/function.strtotime.php

11 Comments

but how do I get the whole date in one timestamp?
@AlejandroCasas like this strtotime('2013/12/22 12:50 PM');
@mahdi why do you accept another answer that is the same as mine and posted minutes later and accept that?
Your original answer was wrong. Your edited answer is closer but still does not show the OP how to put his two variables together in to one string and then convert. The accepted answer provides the exact code to address the OP's problem.
@edwardmp I guess you're a little bit confused. This is not my question, or maybe you meant AlejandroCasas instead of me?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.