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'); 
 ?> 

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'); 
 ?> 

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");

Thanks broj1 for your answer.
Its works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.