Transform DD / MM / YY into PHP timestamp

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 to get to the database as 1514160000 which means "Mon, 25 Dec 2017 00:00:00 GMT"

I have not tried anything because I do not know what and how each function works in PHP

Thanks for any tip

    
asked by anonymous 08.11.2017 / 18:38

3 answers

2

link

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

Notice that I reversed the position of the day and month, it is the inverted North American pattern 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 out several other possible formats!

    
08.11.2017 / 18:46
2
$dateTime = DateTime::createFromFormat('d/m/Y','20/10/2017');

$timestamp = $dateTime->getTimestamp();

In this way, you can create a DateTime object, from any format. link

    
08.11.2017 / 18:52
1

You can use the strtotime () , example:

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

Observing 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);
    
08.11.2017 / 18:51