0

Hi
I woluld lik to add date in my script.
The date should not display just write it in my ip.txt like lis
ip-Date

<?php 
$ip=$_SERVER['REMOTE_ADDR'];
$date= new DateTime("Y-m-d H:i:s");
$file = fopen('ip.txt','a+') or die("can't open file");
fwrite($file,$ip."\n");
echo "Your IP Address Saved: $ip\n";
echo $date->format('Y-m-d H:i:s'); 
 ?> 
2
Contributors
3
Replies
16
Views
3 Years
Discussion Span
Last Post by tony75
0

Now I need the date write into ip.txt file not displaye for user.I got this error
Catchable fatal error: Object of class DateTime could not be converted to string in

<?php 
$ip=$_SERVER['REMOTE_ADDR'];
$dt = new DateTime();
$file = fopen('ip.txt','a+') or die("can't open file");
fwrite($file,$ip,$dt."\n");
echo "Your IP Address Saved: $ip\n";
//echo " Date:",$dt->format('Y-m-d H:i:s'); 
 ?> 
0

On line 5 you are trying to write date object into the text file. It won't go. Also the third argument to the fwrite is optional string leght. I doubt you wanted to use it that way. Change line 5 to something like:

fwrite($file, $ip . ' ' . $dt->format('Y-m-d H:i:s') ."\n");
0

Thanks broj1 for your answer.
Its works :)

Edited by tony75

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.