php - Date and Time pickers to Timestamp field

343

I have a date field and a time field which both have their own pickers. One that has the calendar popup and the other has the time popup. Now I have the calendar's format: dd/mm/yyyy now for some reason when I try to save the inputted values i get:

1969-12-31 00:00:00

So I'm not sure what I am doing wrong, I've read every post I've found here on converting string to timestamp and nothing doing. Here's the code I have:

$mydatetime = strip_tags($_POST['datefield']) . ' ' . strip_tags($_POST['timefield']);
$mydatetime = date("Y-m-d H:i:s", strtotime($mydatetime));

any help please?

100

Answer

Solution:

this is the only code you need (anything else is redundant):

    $mydatetime=date('Y-m-d H:i:s', strtotime("$_POST[datefield] $_POST[timefield]"));
949

Answer

Solution:

Try:

$mydatetime = str_replace('/','-', strip_tags($_POST['datefield'])) . ' ' 
                . strip_tags($_POST['timefield']);
$mydatetime = date("Y-m-d H:i:s", strtotime($mydatetime));

strtotime waiting formatmm/dd/yyyy ordd-mm-yyyy. In your case better transform todd-mm-yyyy

People are also looking for solutions to the problem: php - How to handle multiple api calls on a background job laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.