1

I'm recoding my php script into mysqli and have most of the functions working how I want them to work except for the date and time stamp of an IP connection to my website.

Current code as follows

    <?php






include 'news/admin/dbconnect.php';

$browser  =  $_SERVER['HTTP_USER_AGENT'];

$ip = $_SERVER['REMOTE_ADDR']; //Php retrieves the vistors IP address and stores it in the Var $ip

$visitor = gethostbyaddr($_SERVER['REMOTE_ADDR']); // Php retrives the visitors Host address and stores in the $visitor Var

$page = $_SERVER['PHP_SELF']; // Retrives page name

$sql="INSERT INTO statistics (Page, Browser, Host, IP, Date_time)

VALUES

('$page','$browser','$visitor','$ip', now())";



if (!mysqli_query($DBcon,$sql))

  {

  die('Error: ' . mysqli_error($con));

  }

mysqli_close($con);



?>

But the issue I have is that My script is going to be running in different time zones so I have been attempting to set the time zone wrapped in the $datetime but the output in the database is empty I have searched google and the PHP manual website and I have had a few days of attempts that haven't given the desired out put of "2016-12-07 09:44:14" (this being the server time) but what I'm trying to achieve is to tell my script to get the time in e.g London time zone for myself but if the user is in China the script being configured to run on China time it gives the correct time.

My Last attempt Code:

$datetime->setTimezone(new DateTimeZone('Europe/London'));

I'll be honest here I haven't got a clue what to do I have done a load of google search terms but no dice all results in the database are empty.

Any help would be greatly appreciated.

1 Answer 1

1

you can use this code

<?php

 date_default_timezone_set("Europe/London");
 echo "The time is " . date("h:i:sa");

or

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

as your database filed

5
  • Ok thank you I will give that a go :) the top code block does what is required in getting the London time (will be doing research into accepted time zones) just a thought does the 'date_default_timezone_set' affect the now() function of PHP to be entered that way? Commented Dec 8, 2016 at 18:13
  • It has no affect to the now() Function currently looking on the PHP manual site to other datetime commands Commented Dec 8, 2016 at 18:38
  • you can use php date function as like $date = date('Y-m-d H:i:s); $sql="INSERT INTO statistics (Page, Browser, Host, IP, Date_time) VALUES ('$page','$browser','$visitor','$ip', $date)"; Commented Dec 8, 2016 at 19:03
  • That is ideal thank you I will try your code later as a little busy today haha thank you I will post up later as well �?� Commented Dec 10, 2016 at 13:28
  • had a few minutes to have a coffee lol thank you everyone this answer and its comments have given me the desired outcome thank you all :) Commented Dec 10, 2016 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.