I have a problem that I don't quite understand how to solve, and I came here to see if you can help me.
The thing is that I have the calendar of
and I need to block intermediate dates
example of this would be:
dates from 2022/11/01 to 2022/11/10 and I have to discard the dates from 2022/11/02 to 2022/11/05 and 2022/11/07 to 2022/11/09
and what it should show is in a single input the remaining dates
I mean the dates
2022/11/01,2022/11/06,2022/11/10
<?php
$reservSQL = new ReservSQL();
$reservList = $reservSQL->GetReservbyIdKeeper($idKeeper);
if ($reservList == null) {
?>
<td>
<input type="date" name="dateStart" min="<?php echo $keeper->getTypeUserKeeper()->getDateStart(); ?>" max="<?php echo $keeper->getTypeUserKeeper()->getDateFinish(); ?>" placeholder="START" required>
</td>
<td>
<input type="date" name="dateFinish" min="<?php echo date('Y-m-d', strtotime($keeper->getTypeUserKeeper()->getDateStart() . "+1 day")); ?>" max="<?php echo $keeper->getTypeUserKeeper()->getDateFinish(); ?>" placeholder="FINISH" required>
</td>
<?php
}
elseif($reservList!=null){
$reserv=new Reserv();
foreach ($reservList as $reserv) {}
?>
<tbody>
<td>
<input type="date" name="dateStart"
min="<?php echo date('Y-m-d', strtotime($reserv->getDateFinish() . "+1 day")); ?>"
max="<?php echo $keeper->getTypeUserKeeper()->getDateFinish(); ?>"
placeholder="START" required>
</td>
<td>
<input type="date" name="dateFinish"
min="<?php echo date('Y-m-d', strtotime($reserv->getDateFinish() . "+1 day")); ?>"
max="<?php echo $keeper->getTypeUserKeeper()->getDateFinish(); ?>" placeholder="FINISH" required>
</td>
</tbody>
<?php
}
?>
The thing is that I receive 2 date arrays and I try to get the dates that are reserved using min and max, it gets complicated because it reaches a point where the logic does not do what I want.
I hope you can help me.
I know, I should put more time in programming in PHP, so please find a way to improve this (incomplete) code:
$dates = ['2022/11/01','2022/11/02','2022/11/03','2022/11/04','2022/11/05','2022/11/06','2022/11/07','2022/11/08','2022/11/09','2022/11/10'];
$skip1 = ['2022/11/02','2022/11/05'];
$skip2 = ['2022/11/07','2022/11/09'];
$i = new DateInterval("P1D");
$d = $skip1[0];
while ($d<=$skip1[1]) {
$a=array_search($d,$dates);
unset($dates[$a]);
$t = date_create_from_format('Y/m/d', $d);
print $t->format('Y/m/d')."\n";
date_add($t,date_interval_create_from_date_string("1 days"));
$d = date_format($t,"Y/m/d");
}
var_dump($dates);
When $skip1 contain a start- and end-date, this code will delete that range from $dates. The output:
array(6) {
[0] =>
string(10) "2022/11/01"
[5] =>
string(10) "2022/11/06"
[6] =>
string(10) "2022/11/07"
[7] =>
string(10) "2022/11/08"
[8] =>
string(10) "2022/11/09"
[9] =>
string(10) "2022/11/10"
}
The code should be improved to repeat the step using $skip2, and from the code you will see that I am NOT experienced in PHP.
Related
I'm trying to retrieve a date from a date input form and I just can't get it to work properly. The code I'm using now only returns an error and this date: 1970-01-01. That is not correct and I would like to know how I can approach this. This will be used later to query a database table. I want the date to be basically just a string with this format: "yyyy-mm-dd"
Code:
HTML
<form name="DateFilter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
~~~ EDIT ~~~
Fixed Solution for anyone wondering how it's done:
HTML
<form name="Filter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
<input type="submit" name="submit" value="Login"/>
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
Validate the INPUT.
$time = strtotime($_POST['dateFrom']);
if ($time) {
$new_date = date('Y-m-d', $time);
echo $new_date;
} else {
echo 'Invalid Date: ' . $_POST['dateFrom'];
// fix it.
}
<?php
if (isset($_POST['birthdate'])) {
$timestamp = strtotime($_POST['birthdate']);
$date=date('d',$timestamp);
$month=date('m',$timestamp);
$year=date('Y',$timestamp);
}
?>
I have been searching for a while now, but I cannot put together enough information in order to resolve this problem.
I created an event calendar with a field that allows the user to enter a range for booking an event. I already have the start date configured for the date selected. When a user enters the end date, the information in the fields will be duplicated for each date in the range.
$requester = $_POST['txtrequester'];
$title = $_POST['txttitle'];
$detail = $_POST['txtdetail'];
$startTime = $_POST['txtstarttime'];
$endTime = $_POST['txtendtime'];
$endDate = $_POST['txtenddate']; //end date of range request
$eventdate = $month."/".$day."/".$year;
$today_dt = $year."-".$month."-".$day;
$startDate_dt = strtotime("$year."-".$month."-".$day");
$endDate_dt = strtotime($endDate);
$endDate_dt = $endDate_dt->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($today_dt, $interval, $endDate_dt);
Can someone help me figure this out? Thanks in advance
--Edit--
Code inserting event form data into mysql
$sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate')";
$resultinsert = $connection->query($sqlinsert);
if($resultinsert){
echo "<h4>Event was Successfully Added</h4><br>";
}else{
echo "<h4>Event Failed to be Added</h4><br>" . $sqlinsert;
}
}
Event form html
<form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year;?>&v=true&add=true">
<div class="form-group">
<label for="requester">Requester</label>
<select class="form-control" name='txtrequester'>
<option name='txtrequester'>Melissa</option>
<option name='txtrequester'>Greg</option>
<option name='txtrequester'>Matt</option>
<option name='txtrequester'>Michael</option>
<option name='txtrequester'>Ben</option>
<option name='txtrequester'>Pat</option>
<option name='txtrequester'>Maria</option>
</select>
</div>
<div class="form-group">
<label for="eventTitle">Event Title</label>
<input type="text" class="form-control" id="eventTitle" placeholder="Event Title" name='txttitle'>
</div>
<div class="form-group">
<label for="eventStartTime">Event Start Time</label>
<input type="time" class="form-control" id="eventStartTime" value="12:00" name='txtstarttime'>
</div>
<div class="form-group">
<label for="eventEndTime">Event End Time</label>
<input type="time" class="form-control" id="eventEndTime" value="13:00" name='txtendtime'>
</div>
<a href class="option"><h4>Click here to enter date range (optional)</h4></a>
<div class="form-group range" style="display: none;">
<label for="eventStartDate">Event Start Date</label>
<input type="text" class="form-control" id="disabledInput" name='txtstartdate' disabled value="<?php echo $month . "/" . $day . "/" . $year; ?>">
</div>
<div class="form-group range" style="display: none;">
<label for="eventEndDate">Event End Date</label>
<input type="date" value="<?php echo $year . "-" . $month . "-" . $day; ?>" class="form-control" id="eventEndDate" name='txtenddate'>
</div>
<div class="form-group">
<label for="eventDetail">Event Detail</label>
<input type="text" id="eventDetail" type="text" class="form-control" placeholder="Event Detail" name='txtdetail'>
</div>
<button type="submit" class="btn btn-default" name='btnadd'>Add Event</button>
</form>
It seems you are trying to insert a row in to your data base for each and every date in given date range... Please try it like this
$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );
// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
$given_date = date( 'Y-m-d', $i )."<br>";
// You can put your database insert query here
echo "Data for $given_date is inserted";
}
More efficient way of inserting multiple rows is as follows..
$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );
$sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ";
// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
$eventdate =date('Y-m-d', $i );
// Constructing the insert query
$sqlinsert .= " ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate'),";
}
$bulk_insert_query = rtrim($sqlinsert, ","); // to remove last comma
$resultinsert = $connection->query($bulk_insert_query);
To get more understanding about bulk insert query Try to echo $bulk_insert_query and see how it was constructed and how you want to improve..
Please forgive me, I'm just beginning to study PHP, but I'm pretty well stuck on this sample project that my tutor gave me to figure out.
The goal is to first display the local time of 4 preset timezones, then allow for the user to enter a time manually and on submit, display the time for each of the 4 times relative to the time that was manually entered.
It's also important to do a check that the user input is valid and not just pass any input on regardless of what it may be.
Something is not correct with my IF statement and no input matches, but I haven't been able to see the issue..
Would appreciate if anyone could point me in the right direction. I've been reading thru the documentation on PHP.net quite extensively and have read thru everything on dateTime already but perhaps I'm just not putting it all together yet.
<?php
$timezones = array(
'EST' => -5*3600, //equador
'UTC' => 0, //london
'CCT' => +8*3600, //beijing
'PST' => -8*3600, //los angeles
);
foreach ($timezones as $time) {
$now = time();
$now += $time;
print gmstrftime('DATE: %B %d %Y and TIME: %r <br/>',$now);
}
$nowM = date('m');
$nowD = date('d');
$nowY = date('Y');
$nowHr = date('h');
$nowMin = date('i');
$nowSec = date('s');
?>
<form action="homework2.php" method="post">
<label>Sync Timecode</label>
<input type="text" name="month" placeholder="Month <?php $nowM ?>" value="<?php $nowM ?>" />
<input type="text" name="day" placeholder="Day <?php $nowD ?>" value="<?php $nowD ?>" />
<input type="text" name="year" placeholder="Year <?php $nowY ?>" value="<?php $nowY ?>" />
<input type="text" name="hour" placeholder="Hours <?php $nowHr ?>" value="<?php $nowHr ?>" />
<input type="text" name="min" placeholder="Minutes <?php $nowMin ?>" value="<?php $nowMin ?>"/>
<input type="text" name="sec" placeholder="Seconds<?php $nowSec ?>" value="<?php $nowSec ?>"/>
<input type="submit" id="button">
</form>
<?php
$format = 'd-m-Y h:i:s';
$queryTime = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];
if (date($format,strtotime($queryTime)) == $queryTime) {
$now = new DateTime(strtotime($queryTime), new DateTimeZone('America/Los_Angeles'));
} else {
echo "You entered:" . $queryTime . "<hr style='display:block;'>";
exit;
}
$timezones = array(
'EST' => new DateTimeZone('America/Panama'),
'UTC' => new DateTimeZone('Europe/London'),
'CCT' => new DateTimeZone('Asia/Hong_Kong'),
'PST' => new DateTimeZone('America/Los_Angeles'),
);
echo "You entered:" . $now->format($format) . "<hr style='display:block;'>";
foreach ($timezones as $zone) {
$now = new DateTime($now, new DateTimeZone($zone));
print "The time in" . $zone . "is" . $now->format($format) . "<br/>";
}
?>
your fault shall be forgiven :D
Seriously, I got your code working take a look at the result here:
http://sofia.bplaced.net/homework2.php
It wasn't THAT wrong.
I don't now how reliable this page is, but this shows every change i made:
http://www.diffchecker.com/cfdm0ppc
The new working code:
<?php
$timezones = array(
'EST' => -5*3600, //equador
'UTC' => 0, //london
'CCT' => +8*3600, //beijing
'PST' => -8*3600, //los angeles
);
foreach ($timezones as $time) {
$now = time();
$now += $time;
print gmstrftime('DATE: %B %d %Y and TIME: %r <br/>',$now);
}
$nowM = date('m');
$nowD = date('d');
$nowY = date('Y');
$nowHr = date('h');
$nowMin = date('i');
$nowSec = date('s');
?>
<form action="homework2.php" method="post">
<label>Sync Timecode</label>
<input type="text" name="month" placeholder="Month <?php $nowM ?>" value="<?php $nowM ?>" />
<input type="text" name="day" placeholder="Day <?php $nowD ?>" value="<?php $nowD ?>" />
<input type="text" name="year" placeholder="Year <?php $nowY ?>" value="<?php $nowY ?>" />
<input type="text" name="hour" placeholder="Hours <?php $nowHr ?>" value="<?php $nowHr ?>" />
<input type="text" name="min" placeholder="Minutes <?php $nowMin ?>" value="<?php $nowMin ?>"/>
<input type="text" name="sec" placeholder="Seconds<?php $nowSec ?>" value="<?php $nowSec ?>"/>
<input type="submit" id="button">
</form>
<?php
$format = 'd-m-Y H:i:s';
$queryTime = $_POST['day'] . "-" . date('m',strtotime($_POST['month'])) . "-" . $_POST['year'] . " " . $_POST['hour'] . ":" . $_POST['min'] . ":" . $_POST['sec'];
if (date($format,strtotime($queryTime)) == $queryTime) {
try {
$now = new DateTime(date("c",strtotime($queryTime)));
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
} else {
echo "You entered: " . $queryTime . "<hr style='display:block;'>";
exit;
}
$timezones = array(
'EST' => 'America/Panama',
'UTC' => 'Europe/London',
'CCT' => 'Asia/Hong_Kong',
'PST' => 'America/Los_Angeles',
);
echo "You entered: " . $now->format($format) . "<hr style='display:block;'>";
foreach ($timezones as $zone) {
$now = new DateTime(date("c",strtotime($queryTime)));
date_timezone_set($now, timezone_open($zone));
echo "The time in " . $zone . " is " . $now->format($format." \G\M\TP") . "<br/>";
}
?>
I had to alter the following things:
Your IF-statement was wrong, you compared a 12h Value to a 24H value
line 35:
"h" to "H" //altered hour-format
I expected you wanted your user to enter a month like "April" and not like "04"
line 36:
"$_POST['month']" to "date('m',strtotime($_POST['month']))" //altered month-format
You should always fetch error-exeptions - this way your code may go on in case of an error
line 36:
altered to "try{...}"
line 41:
"You entered: " //added a spacer
line 45 ot 50:
modified the array as you tried to print these in line 56 and used them as parameter in 55
line 52:
"You entered: " //added a spacer
line 55:
"DateTime()" to "date_timezone_set()" //altered the command. The new cmd is part of the same function-set
line 56:
"print" to "echo" //I like uniform code
line 56(2 and 3):
"The time in " and "is " //added spacers
line 56(4):
" \G\M\TP" //relative time to GMT
you wrote something about "...display the time for each of the 4 times relative to the time that was manually entered."
"\G\M\T" will print as "GMT"
"P" will result in the time relative to GMT$
This should work for you.
Aiyion.Prime
I want to be able to separate the birthday from the mysql data into day, year and month.
Using the 3 textbox in html. How do I separate it? I'm trying to think of what can I do with the code below to show the result that I want:
Here's the html form with the php code:
$idnum = mysql_real_escape_string($_POST['idnum']);
mysql_select_db("school", $con);
$result = mysql_query("SELECT * FROM student WHERE IDNO='$idnum'");
$month = mysql_real_escape_string($_POST['mm']);
?>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<tr>
<td width="30" height="35"><font size="2">Month:</td>
<td width="30"><input name="mo" type="text" id="mo" onkeypress="return handleEnter(this, event)" value="<?php echo $month = explode("-",$row['BIRTHDAY']);?>">
As you can see the column is the mysql database is called BIRTHDAY. With this format:
YYYY-MM-DD
How do I do it. So that the data from the single column will be divided into three parts?
Please help thanks,
You can use list and explode to get desired result
list($year,$month,$day)=explode("-", $row['birthday']);
hence $year contains year, $month contains month and $day contains Day, you can use it like this in your text boxes
<input name="mo" type="text" id="mo" value="<?php echo $month;?>">
<input name="dt" type="text" id="dt" value="<?php echo $day;?>">
<input name="yr" type="text" id="yr" value="<?php echo $year;?>">
$parts = explode('-', '1912-03-22');
$parts = split('-', '1912-03-22'); // could also use split()
echo 'Year: ' + $parts[0] . '<br />';
echo 'Month: ' + $parts[1] . '<br />';
echo 'Day: ' + $parts[2] . '<br />';
You could use php's explode function to split the date into its separate compoents:
$birthday ="1981-06-22";
list($year, $month, $day) = explode("-", $birthday);
echo "\nDay $day Month $month Year $year";
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