Strac72 Posted September 13, 2011 Share Posted September 13, 2011 I am using this php to write to a text file to show me who has logged in and when: <?php $time = $_SERVER['REQUEST_TIME']; $myusername = $_POST['myusername']; $data = "$REQUEST_TIME\n"; $data = "$myusername\n"; //open the file and choose the mode $fh = fopen("logs/login.txt", "a") ; fwrite($fh, $data); fwrite($fh, $time); fclose($fh); ?> The result I get in my text file is: paul 1315919200melody 1315919221tracy&graham 1315919232ed&hannah 1315919251 I would feel more successful if the name of the user were to appear on the next line down but I can live with that but I can't make head nor tale of the date. I want it to say: paul 13:59 13/09/2011 next user 14:00 13/09/2011 and so on I feel I have done very well to even get that far really and wouldn't have been able to yesterday. I am frustrated to have reached the extent of my current understanding. If anyone can tell me where I'm going wrong, that would be great. I hope the php isn't too far off, and you can "read between the lines" as it were to see what I'm trying to do. Paul MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/ Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Those are UNIX timestamps. You'll need to use date to convert them into a human-readable format. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268709 Share on other sites More sharing options...
Strac72 Posted September 13, 2011 Author Share Posted September 13, 2011 After a few hours and a lot of swearing, I ended up with this: <?php date_default_timezone_set('GMT+01.00'); $date = date(r); $myusername = $_POST['myusername']; $data = "$myusername\n"; $date = "$date\n"; $fh = fopen("logs/login.txt", "a") ; fwrite($fh, $data); fwrite($fh, $date); fclose($fh); ?> Which has worked, apart from the timezone bit, which still reads as an hour behind. Bloomin' BST. It'll be over in a few weeks! Give a dog a bone! Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268774 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 The date() function, actually most of the date/time functions in php were some of the most confusing to me at first. It gets easier every time you use them . . . Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268778 Share on other sites More sharing options...
Strac72 Posted September 13, 2011 Author Share Posted September 13, 2011 Thanks Pikachu. I gleaned that I'd have to use date_default_timezone_set('Europe/London'); which automatically added the +1 hour I was looking for. I hope it changes back at Halloween or whenever it is. On the road to perfection, I tried creating a $ that was blank to insert a line break into the text file, I tried $blank = '<br />'; and then asking it to fwrite($fh, $blank); but that hasn't worked. Don't really know how to term it for a web search. Crikey, my brain aches! Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268787 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 A linefeed in a text file is typically "\n", <br> is html markup. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268793 Share on other sites More sharing options...
Strac72 Posted September 13, 2011 Author Share Posted September 13, 2011 yes, but when I tried that, it actually wrote: \nuser date&time Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268800 Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 You need to use it in double quotes, not single quotes. Single quotes make it a string literal. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268802 Share on other sites More sharing options...
Strac72 Posted September 13, 2011 Author Share Posted September 13, 2011 "\n\n" Has done the job! Woo hah! I think I'll have a cuppa Thanks very much for the guidance. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268815 Share on other sites More sharing options...
DavidAM Posted September 13, 2011 Share Posted September 13, 2011 You can also use the pre-defined constant PHP_EOL, which contains the proper line ending for the platform PHP is running on. fwrite($fh, PHP_EOL); Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268901 Share on other sites More sharing options...
Strac72 Posted September 13, 2011 Author Share Posted September 13, 2011 I'll look into it tomorrow now. Thanks, David. Quote Link to comment https://forums.phpfreaks.com/topic/247043-server-time-in-unreadable-format/#findComment-1268910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.