简体   繁体   English

使用输入字段将小时和分钟添加到日期时间?

[英]Add hours and minutes to datetime using input fields?

I'm trying to add hours and minutes to the datetime using inputfields. 我正在尝试使用输入字段将小时和分钟添加到日期时间。

the inputfields are within a flash application but they are similar to any other HTML input fields. 输入字段位于Flash应用程序中,但它们与任何其他HTML输入字段相似。

so for the sake of this question, I will be using HTML input fields. 因此,出于这个问题的考虑,我将使用HTML输入字段。

currently I cannot even add the hours to the datetime, let alone the minutes! 目前,我什至无法将小时数添加到日期时间,更不用说分钟了!

My PHP code looks like this: 我的PHP代码如下所示:

$myTime = mysqli_real_escape_string($db_conx, $_POST['myTime']);
$myMinute = mysqli_real_escape_string($db_conx, $_POST['myMinute']);

    $end_date = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s"). ' + '.$myTime.' hours'));

and my input fields are like so: 和我的输入字段是这样的:

    <input type="text" name="myTime" />
    <input type="text" name="myMinute" />

when I enter the $end_date into a MYSQL datetime field, I get this 1970-01-01 01:00:00 当我在MYSQL datetime字段中输入$end_date ,得到的是1970-01-01 01:00:00

but I thought, with my code above, it should be today's date and time + whatever amount's passed through $myTime ? 但是我想,上面的代码应该是today's date and time +通过$myTime传递的金额是$myTime

could someone please advise on this issue? 有人可以建议这个问题吗?

Thanks 谢谢

Use this code. 使用此代码。 Change variables according to you 根据您的需要更改变量

 $minutes_to_add = 5; $h_to_add = 5; $time = new DateTime(); //Here you can also pass your Dynamic Variable like DateTime($variable); $time->add(new DateInterval('PT' . $minutes_to_add . 'M')); $time->add(new DateInterval('PT' . $h_to_add . 'H')); echo $stamp = $time->format('Ymd H:i:s'); 

Try something 试试看

$now = date("Y-m-d H:m:s");
//replace 3 with variable
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now));

//try to use some thing like $minutes = ($hours * 60) + $minutes;
// so you dont need to add separately.
// replace 10 with your variable
$new_time = date("Y-m-d H:i:s", strtotime('+10 minutes', $now));

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

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