简体   繁体   English

当单击按钮时,PHP获取日期和时间并将其存储在变量中

[英]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. 我想知道是否可以使用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! 我对PHP还是很陌生,希望你们能帮助我! 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). 您可以通过向其传递不同的参数来更改date()的格式(例如,切换月份和日期等)。 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. 如果您$date_clicked则日期存储在变量$date_clicked You can do whatever you want with that afterwards, like store it in your MySQL database. 之后,您可以执行任何所需的操作,例如将其存储在MySQL数据库中。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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