Transforming DD/MM/YY into PHP timestamp

Asked

Viewed 399 times

1

I need the user to enter a date and this date is sent to the database as timestamp, example

Insert: 25/12/17 in the field

I want it to reach the database as 1514160000 which means "Mon, 25 Dec 2017 00:00:00 GMT"

I haven’t tried anything because I don’t know what and how each function works in PHP

I appreciate any hint

3 answers

2


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

$timestamp = strtotime('12/25/17');

Notice that I reversed the position of the day and month, is the reversed North American standard of being. And to do this:

$data = '25/12/17';
$dataParcial = explode('/', $data);
$data =  $dataParcial[1] . '/' . $dataParcial[0] . '/' . $dataParcial[2];
$timestamp = strtotime($data);

As @Gabrielheming pointed out in the comments, strtotime will recognize a date in the m/d/y format in the function documentation you can check several other possible formats!

  • Your answer is in the correct format (m/d/y), but will bring an unexpected result to the format (d/m/y) that AP has.

  • Thank you for scoring @Gabrielheming. I was already preparing the issue :)

  • Perfect! Thank you brother

2

1

You can use the strtotime(), example:

$timestamp = strtotime(str_replace('/', '-', '08/11/2017'));

Watching the Supported Date and Time Formats


Using strptime()

$a = strptime('08/11/2017', '%d/%m/%Y');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.