简体   繁体   中英

PHP Get Date and Time when a button is clicked and store it in a variable

I would love to know if this is possible using PHP. For an instance, I want the current date and time be stored on a variable once I click the submit button. Here is what I have in mind:

<?php
if ($_POST["submit"] == 'submit')
//store the variable here
?>

<form action ="" method="post">
<input type="submit" name="submit">
</form>

I am quite new to PHP, hope you guys can help me! cheers.

This might be sufficient for your needs, give it a shot. You can change up the format of date() by passing different parameters to it (like switch up month and day and so on). Check up on that here . Also, don't forget to set your timezone .

<h2>Click</h2>
<form action="" method="POST">
    <button name="click" class="click">Click me!</button>
</form>

<?php
if(isset($_POST['click']))
{
    $date_clicked = date('Y-m-d H:i:s');;
    echo "Time the button was clicked: " . $date_clicked . "<br>";
}
?>

The date is stored in the variable $date_clicked if you didn't notice. You can do whatever you want with that afterwards, like store it in your MySQL database.

you can do it like this:

<?php

if (isset($_POST["submit"])){

 // getting current Date Time OOP way
 $currentDateTime = new \DateTime();

 //set timeZone
 $currentDateTime->setTimezone(new \DateTimeZone('America/New_York'));
 $dateTime = $currentDateTime->format('l-j-M-Y H:i:s A');

}

?>

<form action ="" method="post">
<input type="submit" name="submit">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM