Function to show current date

0

I have two lines to return the current date in date format and not in string, but it does not return anything:

$date = date("Y-m-d H:i:s");
$hoy = new DateTime($date);

Any help?

    
asked by Xerox 22.10.2018 в 16:20
source

2 answers

0

I understand that to show the date using date it is necessary to make the declaration as it is done in the documentation, in this way:

<?php
// Establecer la zona horaria predeterminada a usar. Disponible desde PHP 5.1
date_default_timezone_set('UTC');


// Imprime algo como: Monday
echo date("l");

// Imprime algo como: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

// Imprime: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

/* Usar las constantes en el parámetro de formato */
// Imprime algo como: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);

// Imprime algo como: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>
    
answered by 22.10.2018 в 16:44
0

As they said in the previous answer, you still need to define the date format of your date:

date_default_timezone_set('America/Lima');
$hoy = date("Y-m-d");
$date = new DateTime($hoy );
// En la siguiente linea defines la manera como quieres que se muestr tu fecha, puedes agregar o quitar los campos que desees,segun tus necesidades. Por ejemplo:
echo $date->format('l jS \of F Y h:i:s A');
    
answered by 22.10.2018 в 17:46