user7844662
user7844662

Reputation:

Getting the real time and date in PHP

i am creating a webpage and i have a form inside of it. In one of my input box i want to set the current date automatically without the user inputting it. I am using the getDate() function of PHP but the problem is that people can change the date by changing their system date and time....

I want the real date and time.

$mydate=getdate(date("U"));
$date = "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";
<input value='$date'>

Help Guys

Upvotes: 3

Views: 4434

Answers (5)

Senthuran Ambalavanar
Senthuran Ambalavanar

Reputation: 157

PHP date() function gets time from your system. So that a user can change his system time and data will have that time.

A solution would be, have a Timestamp field in your database table (I'm assuming that you are going to save the form data in a database) and that will take the time from the server, where the database is hosted.

The user can't change the server's time, so that a correct time will be updated.

Upvotes: 3

Gowthamy Vaseekaran
Gowthamy Vaseekaran

Reputation: 147

You can use date() function to set current date and time automatically.
For example

 <?php echo '<input type=text value="' . date("d F Y H:i:s") . '" readonly/>';?>

d : Day of the month, 2 digits with leading zeros (01-31)
F: A full textual representation of a month, such as January or March
Y: full numeric representation of a year, 4 digits (2017)
H: 24-hour format of an hour with leading zeros (00 through 23)
i: Minutes with leading zeros (00 to 59)
s:Seconds, with leading zeros (00 to 59)

Upvotes: 2

Stender
Stender

Reputation: 2492

No real need to have it in the form - You can just handle the date on the other side of the post method. like:

sql = CURDATE()
php = date("Y-md"); //or what ever format you fancy.

Upvotes: 0

Hossain Azad
Hossain Azad

Reputation: 62

Create a input like

<input type="text" name="date" value="<?php echo date("Y-m-d H:i:s");?>" disabled>

More details about date function available at http://php.net/manual/en/function.date.php

Upvotes: 0

Shakti Phartiyal
Shakti Phartiyal

Reputation: 6254

The code you have provided sets the dat according to the server's date and time and in not affected by the users' system date and time: The one thing you can add so that the user cannot modify the date is the readonly flag:

$mydate=getdate(date("U"));
$date = "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";

The readonly attribute can be used like so:

<input value="<?php echo $date;?>" readonly="readonly">

OR

<input value="<?php echo $date; ?>" readonly>

If your code is inside php script then:

echo '<input value="'.$date.'" readonly>';

Upvotes: 0

Related Questions