Date and Time in Datetime format

2

How do I concatenate these two variables to stay in ISO format (Y-m-d H: i: s)?

$data = '17-04-2018';
$hora = '16:12';
    
asked by anonymous 17.04.2018 / 17:15

2 answers

4

You can do the following:

1 - We use strtotime () to convert to unix timestamp ;
2 - We use date () to convert this timestamp to the desired format:

<?php
$data = '17-04-2018';
$hora = '16:12';

$timestamp = strtotime($data. ' ' .$hora);
echo date('Y-m-d H:i:s', $timestamp); // 2018-04-17 16:12:00

DEMONSTRATION

    
17.04.2018 / 17:19
5

You can use these two functions:

date_create link

date_format link

$data = '17-04-2018';
$hora = '16:12';

$date = date_create($data . $hora);
$result = date_format($date, 'Y-m-d H:i:s');

att,

    
17.04.2018 / 17:21