Alejandro
Alejandro

Reputation: 1159

PHP get timestamp from separate date and time

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

Upvotes: 1

Views: 1024

Answers (3)

Darragh Enright
Darragh Enright

Reputation: 14136

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

Upvotes: 1

Charlie74
Charlie74

Reputation: 2903

Give this a try...

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

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

$timestamp = strtotime($full_date);

Upvotes: 1

edwardmp
edwardmp

Reputation: 6611

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

Upvotes: 0

Related Questions