PHP countdown timer using 'd-m-Y H:i:s' timestamp - php

Im trying to create a timer that for 1 hour from a timestamp it shows a count down then after that shows a form button but I can't get it to work, I have tried multiple methods I found online but they either display the wrong time, or never show the form. Here is what I have so far:
while($row2 = $result2->fetch_assoc()) {
$timestamp = $row2['timestamp'];
$work_id = $row2['work_id'];
$current_time = date("Y-m-d H:i:s");
$ready_time = date("Y-m-d H:i:s", strtotime($timestamp)+3600);
if($current_time > $ready_time){?>
<form action="" method="POST" onsubmit="return confirm('Are you sure?');">
<input type="hidden" value="<?echo $work_id;?>" name="work_id">
<input type="submit" name="submit" value="Complete Workorder">
</form>
?
}else{
$time1 = new DateTime($current_time);
$time2 = new DateTime($timestamp);
$interval = $time1->diff($time2);
echo $interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds";
}
}

You can calculate the time left in your sql:
SELECT work_id, TIMESTAMPDIFF(SECOND,CURRENT_TIMESTAMP(),`timestamp`) AS timeleft
FROM tablename
Then check that value in your php:
$timeleft = $row2['timeleft'];
$work_id = $row2['work_id'];
if($timeleft >=0): ?>
<form action="" method="POST" onsubmit="return confirm('Are you sure?');">
<input type="hidden" value="<?echo $work_id;?>" name="work_id">
<input type="submit" name="submit" value="Complete Workorder">
</form>
<?php else: ?>
<p>You need to wait <?php echo $timeleft;?> seconds</p>
<?php endif;
If you want to chow a countdown that actually updates without a refresh, you will need to use javascript, in place of <p>You need to wait <?php echo $timeleft;?> seconds</p>

Related

PHP post - converting time in form

i want to build a site, where a user can choose a timeslot. This timeslot should be printed on the following site. But i have a problem with the correct presentation or displaying the chosen time.
Here my code so far:
Site1, where the user choose a timeslot:
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>
Site2, where the chosen time should be printed:
<?php
$new_date = date('H:00', strtotime($_POST['Zeit']));
echo $new_date;
?>
But with this i get everytime the output of "01:00".
Please try below code
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit' value='".date("H:00", mktime($time+0,25))."'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>

How to echo all dates between two dates?

How to echo all dates between two dates?
for example:
From the date of 2020/07/29 to the date of 2020/08/02
I want to print the results
2020/07/29
2020/07/30
2020/07/31
2020/08/01
2020/08/02
The following code prints only to 2020/07/31
if(isset($_POST['submit'])){
$from_date= $_POST['from_date'];
$to_date = $_POST['to_date'];
for($from_date = $from_date ; strtotime("+$from_date Day") <= $to_date; $from_date++){
echo $from_date . "<br>";
}
}
Result:
2020/07/29
2020/07/30
2020/07/31
HTML FORM
<form action="" method="post">
<input type="date" name="from_date">
<input type="date" name="to_date" >
<input name="submit" type="submit" value="submit">
</form>
Thanks

Check date is today's date or yesterday's date

I want to insert data into table when selected date is Current Date or Yesterday's Date.
html code:
<input type="text" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="<?php echo date('d/m/Y'); ?>">
php code:
if(isset($_POST['submit'])){
$report_date=date('Y-m-d',strtotime($_POST['reportdate']));
$check_date=date('d',strtotime($_POST['reportdate']));
if($check_date!=date('d') or $check_date!=date('d')-1){
echo "<h2>You can only submit dcr of current date or yesterday.</h2>";
}
else{
$client=$_POST['CLIENT_NAME'];
//$cars_string = implode(', ', $_POST['cars']);
$productname_string=implode(', ',$_POST['productname']);
$remarks=$_POST['remark'];
$sql="insert into d_c_r (bm,date_time,client,promoted_product,remark) values ('".$_SESSION['id']."','$report_date',$client,'$productname_string','$remarks')";
$res=mysql_query($sql) or die(mysql_error());
}
}
Working Code Updated:
Working Demo: http://phpfiddle.org/main/code/fxh8-gnkw
<html>
<body>
<form action="" method="post" name="myform">
<input type="date" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="">
<button type="submit" name="submit"> Submit</button>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && !empty($_POST['reportdate'])){
$today_date = date('d-m-Y'); // you get current date
$date_tday = new DateTime($today_date);
$date_tday->modify('-1 day');
$get_yesterday_date_from_current = $date_tday->format('d-m-Y'); // you get Yesterday date that from current date
$reportdate = $_POST['reportdate']; // you get report date that user is selected
$report_date_format = new DateTime($reportdate);
echo $report_date = $report_date_format->format('d-m-Y'); //you get report date that user is selected in d-m-Y
/*
$date = new DateTime($report_date);
$date->modify('-1 day');
echo $get_yesterday_date = $date->format('d-m-Y'); // you get Yesterday date that user is selected
*/
if($report_date !== $today_date && $report_date !== $get_yesterday_date_from_current){
echo "<h2>You can only submit dcr of current Date or Yesterday.</h2>";
}
else{
echo "Inser Your Data OR What You WAnt To Do HERE";
}
}
?>

Substracting birthday year with the current year

I've started learning PHP, and just can't figure out how to accomplish this.
The basic question is to how to calculate age from current year..
I have to do it with a submit button, and that's the problem for me. I tried this:
<form name="form2" action="16.1.php" method="POST">
Birthday year:
<input type="post" name="post">
<input type="submit" value= "Verstuur" >
</form>
16.1php:
<?php
echo date ('Y') - $_POST['post'];
?>
Try updating this..
<input type="input" name="post">
type = "post" to type = "input"
Try this
if($_POST['submit'])
{
$byear = $_POST['bdate'];
$today = new DateTime();
$birthdate = new DateTime($byear);
$interval = $today->diff($birthdate);
echo $interval->format('%y years');
}
<form name="form2" action="" method="POST">
Birthday year:
<input type="text" name="bdate">(yyy-mm-dd)
<input type="submit" name="submit" value= "Verstuur" >
</form>

Form Submitting Incorrect Information to MySQL Database

I've created a form that submits data to a MySQL database but the Date, Time, Year and Month fields constantly revert to the exact same date (1st January 1970) despite the fact that when I submit the information to the database the form displays the current date, time etc to me. I've already set it so that the time and date fields automatically display the current time and date. Could someone please help me with this.
Form:
<html>
<head>
<title>Ultan's Blog | New Post</title>
<link rel="stylesheet" href="css/newposts.css" type="text/css" />
</head>
<body>
<div class="new-form">
<div class="header">
<img src="images/edit-home-button.png">
</div>
<div class="form-bg">
<?php
if (isset($_POST['submit'])) {
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
mysql_connect ('localhost', 'root', 'root') ;
mysql_select_db ('tmlblog');
$sql = "INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')";
$result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());
if ($result != false) {
print "<p class=\"success\">Your entry has successfully been entered into the blog. </p>";
}
mysql_close();
}
?>
<?php
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
<input type="text" name="time" id="time" size="5"value="<?php echo $current_time; ?>" />
<input class="field2" type="text" id="title" value="Title Goes Here." name="title" size="40" />
<textarea class="textarea" cols="80" rows="20" name="entry" id="entry" class="field2"></textarea>
<input class="field" type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</div>
<div class="bottom"></div>
<!-- //End Wrapper!-->
</body>
</html>
</html>
For some reason the posts are being submitted without a timestamp and are reverting to a default timestamp.
Sounds like your form is giving your database a very low amount of "Ticks". Notice you have an with name="date" and id="date" three times. This is most likely your problem. Change those names and ids, do use month, year, date (according to your "postback").
You have three text boxes named date. Since $_POST['month'] and $_POST['year'] are not set, strtotime() cannot figure out the correct timestamp and the SQL query looks like this:
INSERT INTO php_blog (timestamp,title,entry) VALUES ('','Title Goes Here.','')
Change the names of the input elements to what your PHP code is expecting:
<input type="text" name="month" value="<?php echo $current_month; ?>" />
<input type="text" name="date" value="<?php echo $current_date; ?>" />
<input type="text" name="year" value="<?php echo $current_year; ?>" />
After you've done this, strtotime() will be able to parse the date and insert the correct value into the query.
INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','')
It could be that strtotime fails to recognize the format you are submitting your date in.
Try a classic YYYY/MM/DD format:
$timestamp = strtotime("$year/$month/$date $time");
Looks like strtotime don't understand the format you feeding to it.
Use mktime instead.
Anyway you have to always debug your code, i.e. print out the query to see if anything goes wrong.
Or consider to use mysql-supported type colymn, date or datetime
You may be confused an unix timestamp which is just number ans mysql timestamp type column which is string of 2010-05-01 00:00:00

Categories