Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert date and time to timestamp in php?

i have a date '07/23/2009' and a time '18:11' and i want to get a timestamp out of it : here is my example:

date_default_timezone_set('UTC');

$d = str_replace('/', ', ', '07/23/2009');
$t = str_replace(':', ', ', '18:11');
$date = $t.', 0, '.$d;
echo $date;
echo '<br>';
echo $x = mktime("$date");

the issue is that $x gives me the current timestamp.

any ideas?

like image 542
Patrioticcow Avatar asked Dec 16 '22 08:12

Patrioticcow


1 Answers

it gives error because mktime function require all values of numbers only and this function gives only date . if you try like

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

then it will work.

so your complete code will be

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//if you want timestamp then use

echo strtotime("07/23/2009 18:11");

Thanks

like image 195
divyang asodiya Avatar answered Jan 06 '23 14:01

divyang asodiya