Le format d'affichage des dates par défaut est d/m/Y
| |
ID |
Date |
DateTime |
Timestamp |
Year |
Int timestamp |
Short date |
Inverse Short Date |
Short date Txt |
Text timestamp |
Date slash |
| Format SQL |
|
%Y-%m-%d |
%Y-%m-%d %H:%i:%s |
%Y-%m-%d %H:%i:%s |
|
DO NOT USE IT |
%Y%m%d |
%d%m%Y |
%Y%m%d |
DO NOT USE IT |
%d/%m/%Y |
| Format PHP |
|
d/m/Y |
d/m/Y H:i:s |
Y-m-d H\h i\m\n s\s |
|
d/m/Y |
d/m/Y |
d/m/Y |
d/m/Y |
d/m/Y |
d/m/Y |
| Objet |
1 |
1616112000 |
1616174727 |
1616174727 |
2016 |
1616174727 |
1616112000 |
1616112000 |
1616112000 |
1616174727 |
1616112000 |
| VIEW |
1 |
19/03/2021 |
19/03/2021 17:25:27 |
2021-03-19 17h 25mn 27s |
2016 |
19/03/2021 |
19/03/2021 |
19/03/2021 |
19/03/2021 |
19/03/2021 |
19/03/2021 |
ExamplesDates.class.php
<?php
/**
* @author Richaud Julien "Fladnag"
*/
use salt\Base;
use salt\Field;
use salt\SqlDateFormat;
/**
* // @property annotations are used only for IDE autocomplete
* // there are not used by SALT framework
* @property int $id
* @property int $date
* @property int $datetime
* @property int $timestamp
* @property int $year
* @property int $int_timestamp
* @property int $ymd
* @property int $dmy
* @property int $ymd_text
* @property int $timestamp_text
* @property int $date_slash // date_slash is a VARCHAR in MySQL, but in objet it's a timestamp, so, it's an int
*/
class ExamplesDates extends Base {
public function metadata() {
parent::MODEL()
->registerId('id')
->registerTableName('test_dates')
->registerFields(
Field::newNumber('id', 'ID') ->sqlType('INT PRIMARY KEY AUTO_INCREMENT'),
Field::newDate('date', 'Date', SqlDateFormat::DATE) ->sqlType('DATE'),
Field::newDate('datetime', 'DateTime', SqlDateFormat::DATETIME, 'd/m/Y H:i:s') ->sqlType('DATETIME'),
Field::newDate('timestamp', 'Timestamp', SqlDateFormat::TIMESTAMP, 'Y-m-d H\h i\m\n s\s')->sqlType('TIMESTAMP'),
// une annee ne peut etre convertie en timestamp. On ne peux donc pas le gérer comme une date
Field::newNumber('year', 'Year') ->sqlType('YEAR'),
Field::newDate('int_timestamp', 'Int timestamp', SqlDateFormat::RAW_TIMESTAMP) ->sqlType('INT(11)'),
Field::newDate('ymd', 'Short date', SqlDateFormat::SHORT_DATE) ->sqlType('INT(11)'),
Field::newDate('dmy', 'Inverse Short Date', '%d%m%Y') ->sqlType('VARCHAR(11)'),
Field::newDate('ymd_text', 'Short date Txt', SqlDateFormat::SHORT_DATE) ->sqlType('VARCHAR(11)'),
Field::newDate('timestamp_text', 'Text timestamp', SqlDateFormat::RAW_TIMESTAMP)->sqlType('VARCHAR(11)'),
Field::newDate('date_slash', 'Date slash', '%d/%m/%Y') ->sqlType('VARCHAR(14)')
);
}
public function initAfterCreateTable(salt\DBHelper $db) {
$o = new ExamplesDates();
// le format de stockage des dates en PHP est TOUJOURS un timestamp
$o->date = time();
$o->datetime = time();
$o->timestamp = time();
$o->year = 2016; // l'année n'est pas considérée comme une date
$o->int_timestamp = time();
$o->ymd = time();
$o->dmy = time();
$o->ymd_text = time();
$o->timestamp_text = time();
$o->date_slash = time();
return $o;
}
}
dateFormat.php
<?php
/**
* @author Richaud Julien "Fladnag"
*/
define('TITLE', 'Format dates');
include('../../lib/base.php');
use salt\DatabaseHelper;
use salt\DBHelper;
use salt\Query;
use salt\SqlDateFormat;
include(RELATIVE.'examples/dao/ExamplesDates.class.php');
$DBtest = DBHelper::getInstance(DB_DATABASE_TEST);
$missings = DatabaseHelper::missingTables($DBtest, array('ExamplesDates'));
if (count($missings) > 0) {
DatabaseHelper::createTablesFromObjects($DBtest, $missings);
}
$q = ExamplesDates::query(TRUE);
$q->whereAnd('id', '=', 1);
$r = $DBtest->execQuery($q);
/**
* @var ExamplesDates $testDate
*/
$testDate = \salt\first($r->data);
include(RELATIVE.'examples/layout/header.php');
?>
<div>Le format d'affichage des dates par défaut est <?= salt\DEFAULT_DATE_DISPLAY_FORMAT ?></div>
<table style="white-space: nowrap">
<tr>
<th> </th>
<?php foreach($r->columns as $col) {?>
<th><?= ExamplesDates::COLUMN()->$col ?></th>
<?php } ?>
</tr>
<tr>
<th>Format SQL</th>
<?php foreach($r->columns as $col) { ?>
<td><?= SqlDateFormat::resolve($testDate::MODEL()->{$col}->format) ?></td>
<?php } ?>
</tr>
<tr>
<th>Format PHP</th>
<?php foreach($r->columns as $col) { ?>
<td><?= $testDate::MODEL()->{$col}->displayFormat ?></td>
<?php } ?>
</tr>
<tr>
<th>Objet</th>
<?php foreach($r->columns as $col) { ?>
<td><?= $testDate->$col ?></td>
<?php } ?>
</tr>
<tr>
<th>VIEW</th>
<?php foreach($r->columns as $col) { ?>
<td><?= $testDate->VIEW->$col ?></td>
<?php } ?>
</tr>
</table>
<div>
<h3>ExamplesDates.class.php</h3>
<?php highlight_file(RELATIVE.'examples/dao/ExamplesDates.class.php'); ?>
<br/>
<h3><?= \salt\last(explode(DIRECTORY_SEPARATOR, __FILE__)) ?></h3>
<?php highlight_file(__FILE__); ?>
</div>
<?php include(RELATIVE.'examples/layout/footer.php');