Date and Time in Datetime format

Asked

Viewed 892 times

2

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

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

3 answers

6

  • 1

    Both this and the other answer worked perfectly. Thank you

5


Here’s what you can do:

1 - We use strtotime() to convert into 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

  • 3

    It worked great. Thanks for the demonstration.

-4

I have a great job here in case anyone needs it.

function formatadata($data){
setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
return strftime('%A, %d de %B de %Y %H:%M:%S' , strtotime($data));}
  • 2

    This code does not answer the question the user wants to know how to concatenate two strings to form a timestamp.

Browser other questions tagged

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