View PHP date and time

Asked

Viewed 15,217 times

6

I am trying to display the exact time with PHP in the following format.

Date: YYYY-MM-DD

Time: 00:00:00:000

  <?php 
       setlocale( LC_ALL, 'pt_BR', 'pt_BR.iso-8859-1', 'pt_BR.utf-8', 'portuguese' ); 
       date_default_timezone_set( 'America/Sao_Paulo' );
       echo strftime( '%Y-%m-%e %T', strtotime('today')); 
?>

With the current code it according to my research would display this for example, 2015-01-26 00:00:00

I am using Windows7 x64 and PHP Version 5.5.8 and the page displays nothing, nor errors.

  • And what’s the problem?

  • 1

    @Jorgeb. the generated page shows nothing, no errors

  • In operating system you are using?

  • Have you seen the log? Have warnings and errors appear on the page? Or just in the log?

  • @rray windows 7 x64

  • I could not reproduce the problem http://ideone.com/eG9gmq

  • It also works well here: http://phpfiddle.org/main/code/w1f0-b7ij

  • 2

    the %e not supported by windows, strftime swap for %d.

  • @rray the %d is also not supported? I am trying to use it but nothing happens!

  • I did some tests here, it only worked with the %d and also removing the %T.

  • Ask the question the php version tbm.

  • @rray the date is right, I will try to solve the time, obg

  • Who will answer?

  • @moustache sorry I didn’t understand!

  • The @rray got the message.

  • If you do not need to format the date located (translated) the function date() it seems simpler.

  • I always thought the function date() took the local time and date

Show 12 more comments

4 answers

8


The problem is the function strftime, that does not make the timestamp conversion properly in windows, some arguments like alert documentation are not supported:

%e, %T, %R, %D

For your code to work %e and %T for,

%d for the day with two digits 01-31.

%H to display the time in the 00-23 format

%M for minutes in format 00-59

%S For second format 00-59

The code should stay that way:

echo strftime( '%Y-%m-%d %H:%M:%S', strtotime('today') );

List of supported arguments in windows - msdn

2

Try:

<?php echo date('Y-m-d H:i:s') ?>

1

Your problem is in strtotime what you put today, translating, hoje. Try to put now (agora).

echo strftime( '%Y-%m-%e %T', strtotime('now'));
  • 2

    had already tried, continued the same way problems were even related to strftime.

1

To display: Date: YYYY-MM-DD and current time:

<?php

    $date = date('Y-m-d H:i:s:u');
    echo $date;


?>

Browser other questions tagged

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