Jump to content

Edit form not displaying time and date from database


Go to solution Solved by wongle,

Recommended Posts

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.

dt.png

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.

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']))?>">

 

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

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 by phppup
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 by mac_gyver

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>

image.png.b3416dd66d270cdc409ba40236e99d35.png

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.