php time not updating

rwahdan1978

New member
Hi

I Have a PHP code to display the time in my html.
Code:
<?php
$time = time();
$file = 'index.html';
file_put_contents($file, $time);
?>

I am always getting the same value even if i refresh the page.
check out the page:

omarbinkhataab.com/test/index.html
 
You need to execute the php file if you want to execute the code.
You can use simply
Code:
<?php
echo time();
If you want to use one script for writing the date to another one you need to open both in right order
 
To execute the code, you will need to run the PHP file. Here's a simple example:

<?php
echo time();

If you want to write the date from one script to another, you should ensure that you open both scripts in the correct order.
 
Last edited:
The PHP code you provided will write the current Unix timestamp (the number of seconds since January 1, 1970, 00:00:00 UTC) to a file named index.html. If you want to display the time in a human-readable format in your HTML code, you can use the date() function to format the timestamp as follows:

<?php
$time = time();
$formatted_time = date('Y-m-d H:i:s', $time);
echo "The current time is: " . $formatted_time;
?>

This will output the current time in the format of "YYYY-MM-DD HH:MM:SS". You can modify the format string passed to the date() function to display the time in a different format. For example, to display the time in a 12-hour format with AM/PM indicator, you can use the following format string:

$formatted_time = date('h:i:s A', $time);

This will output the time in the format of "HH:MM:SS AM/PM".
 
Hello this is Gulshan Negi
Well, it may be because of a coding error, let's try executing the below command.

<?php
echo time();

Well, I have taken its reference from here, where the author lists PHP date and time commands with examples and outputs. If you have any doubts then you can check about it.
Thanks
 
Back
Top