This is a table, it has a field named register_date
which is automatically added by DB as a TimeStamp.
I want to show this field as a Persian DateTime. like this image below:
and here is my php piece of code:
<?php
$q = "SELECT * from `tbl_registers2`";
$result = $db->query($q);
$row = 1;
while ($fields = $result->fetch_assoc()) {
?>
<tr data-id="<?= $fields['id']; ?>">
<td><?= $row ; ?></td>
<td><?= $fields['first_name']; ?></td>
<td><?= $fields['last_name']; ?></td>
<td><?= $fields['mobile']; ?></td>
<td><?= $fields['email']; ?></td>
<td><?= $fields['register_date']; ?></td>
<td><img src="../assets/img/trash.png"></td>
<td>
<a href="edit.php?id=<?= $fields['id']; ?>">edit</a>
</td>
</tr>
<?php
}
?>
how to do this?
CodePudding user response:
I've found the solution myself after 3 hours.
the solution is to use the IntlDateFormatter
class of PHP like this:
<?php
date_default_timezone_set('Asia/Tehran');
$formatter=new IntlDateFormatter(
'fa-IR@calender=persian',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Asia/Tehran',
IntlDateFormatter::TRADITIONAL,
"yyyy-MM-d H:m:s"
);
$q = "SELECT * from `tbl_registers2`";
$result = $db->query($q);
$row = 1;
while ($fields = $result->fetch_assoc()) {
$dateTime = datetime::createfromformat('Y-m-d H:i:s',$fields['register_date']);
?>
<tr data-id="<?= $fields['id']; ?>">
<td><?= $row ; ?></td>
<td><?= $fields['first_name']; ?></td>
<td><?= $fields['last_name']; ?></td>
<td><?= $fields['mobile']; ?></td>
<td><?= $fields['email']; ?></td>
<td><?= $formatter->format($dateTime); ?></td>
<td><img src="../assets/img/trash.png"></td>
<td>
<a href="edit.php?id=<?= $fields['id']; ?>">edit</a>
</td>
</tr>
<?php
}
?>
createfromformat
from datetime
class is used to convert a plain date string which is read from database to a datetime object.
after all, $formatter->format($dateTime);
shows the results.
to use IntlDateFormatter
class, extension=php_intl.dll
must be placed in the php.ini
.