wongle Posted June 24, 2022 Share Posted June 24, 2022 I have wrote an edit script to edit events in a calendar and I am unable to get the time and date to display from the database. I am able to get the date to display without the time, but when I add the time element back in, it shows nothing on the edit page fields. $title = isset($_POST['title']) ? $_POST['title'] : $callEvents['title']; $contacts = isset($_POST['contacts']) ? json_encode($_POST['contacts']) : $callEvents['contacts']; $start_date = isset($_POST['start_date']) ? $_POST['start_date'] : $callEvents['start_date']; $end_date = isset($_POST['end_date']) ? $_POST['end_date'] : $callEvents['end_date']; $color = $callEvents['color']; $description = isset($_POST['description']) ? $_POST['description'] : $callEvents['description']; $status = 1; $stmt = $pdo->prepare('UPDATE calendars SET title = ?, contacts=?, start_date=?,end_date=?, color = ?, status = ?, description = ?, created_at = ?, updated_at = ? WHERE id = ?'); $result = $stmt->execute([$title, $contacts, $start_date,$end_date, $color, $status, $description,date("Y/m/d"),date("Y/m/d"), $_GET['id']]); From form: <input type="datetime-local" class="form-control datetime" name="end_date" value="<?=date('Y-m-d H:i:s', strtotime($callEvents['end_date']))?>"> The database fields for end and start date are set to datetime I know I am doing something stupid somewhere, I just can't see the mistake. Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/ Share on other sites More sharing options...
Barand Posted June 24, 2022 Share Posted June 24, 2022 As you are having a problem with the retrieval and display it might be an idea to show that portion of the code. As an aside, you are overwriting your date created with the date updated (and wrong format). Define those fileds in your table as created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, the omit them from your insert and update queries - they will update automatically. Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597599 Share on other sites More sharing options...
kicken Posted June 24, 2022 Share Posted June 24, 2022 Output the values you get separately so you can verify if they are what you expect. <p>End date: <?=$callEvents['end_date']?></p> <p>End timestamp: <?=strtotime($callEvents['end_date'])?></p> <input type="datetime-local" class="form-control datetime" name="end_date" value="<?=date('Y-m-d H:i:s', strtotime($callEvents['end_date']))?>"> Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597604 Share on other sites More sharing options...
wongle Posted June 24, 2022 Author Share Posted June 24, 2022 18 minutes ago, kicken said: Output the values you get separately so you can verify if they are what you expect. <p>End date: <?=$callEvents['end_date']?></p> <p>End timestamp: <?=strtotime($callEvents['end_date'])?></p> <input type="datetime-local" class="form-control datetime" name="end_date" value="<?=date('Y-m-d H:i:s', strtotime($callEvents['end_date']))?>"> It comes back with End date: 2022-06-24 14:56:00 End timestamp: 1656078960 Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597605 Share on other sites More sharing options...
mac_gyver Posted June 24, 2022 Share Posted June 24, 2022 the format for the value of a datetime-local field is - YYYY-MM-DDThh:mm ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597606 Share on other sites More sharing options...
phppup Posted June 25, 2022 Share Posted June 25, 2022 (edited) The TIMESTAMP that you have is a countdown of seconds that can be converted to "human" readability by reformatting it. This should get you on the correct path $date = date('d-m-Y H:i:s', 1565600000); echo "The date is $date."; Note that 1565600000 is a test TIMESTAMP. You can research DATE formatting for a variety of effects to organize the date information in a way that you prefer. ie year- month- date or otherwise. You can even spell out the names of months etc. Edited June 25, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597612 Share on other sites More sharing options...
wongle Posted June 27, 2022 Author Share Posted June 27, 2022 Thanks for all the advice guys, been a massive help. Updated values to d-m-Y H:i:s I have noticed that the code is working in Firefox on Windows 11 but not Chrome. Works fine on Windows 10 in any browser. Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597639 Share on other sites More sharing options...
mac_gyver Posted June 27, 2022 Share Posted June 27, 2022 (edited) 23 minutes ago, wongle said: d-m-Y H:i:s that's not the correct format, resulting in different browser/operating system/local system defaulting to what they think they should do. did you read the information at the developer.mozilla.org link that was given? Edited June 27, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597640 Share on other sites More sharing options...
Solution wongle Posted June 28, 2022 Author Solution Share Posted June 28, 2022 Sorted - Y-m-d\TH:i:s Now working fine Thanks for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597653 Share on other sites More sharing options...
mac_gyver Posted June 30, 2022 Share Posted June 30, 2022 if you look again, the seconds are not used. the php date format string would be - Y-m-d\TH:i Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597745 Share on other sites More sharing options...
Barand Posted June 30, 2022 Share Posted June 30, 2022 Seconds are optional <form> dt1 <input type='datetime-local' name='dt1' value='<?= date('Y-m-d\TH:i') ?>' > <br> dt2 <input type='datetime-local' name='dt2' value='<?= date('Y-m-d\TH:i:s') ?>' > <br> <input type='submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/314960-edit-form-not-displaying-time-and-date-from-database/#findComment-1597749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.