Dealing with and formatting dates on Yii

# Dealing with and formatting dates on Yii

Last updated:

You should always define attributes like create_date and possibly last_update for all your database columns. They're very useful for logging and for extracting metrics from your database and sooner or later you'll need them so you might as well start off with them.

if field create_date is of type DATETIME on a db, you can do this

<?php
$model = new MyModel(); // mysql-friendly format$model->create_date = date('Y-m-d H:i:s');
$model->save();  ### Configure a CGridView column to display formatted dates <?php 'columns'=> array( array( 'name'=>'create_date', 'value'=>'date_format(new DateTime($data->create_date),"d/m/Y")',
'filter'=>false,
'sortable'=>false
),
//..more columns


### Displaying formatted dates in a CDetailView widget

<?php
array(
'name'=>'create_date',
// this is just an example format constant. You can use regular formats as well.
'value'=>date_format(new DateTime($model->create_date),DATE_RFC850) )  ### Setting a default value in a form model for a DATETIME attribute say you have a 'creation_date' attribute for which you want to set a default value of today's date and time: edit the rules() method in your model: <?php public function rules() { return [ ['date_created','default','value'=>date('Y-m-d H:i:s')], // mysql format ]; }  ### Comparing dates From my experience, the easiest and most accurate way to compare dates (for example, to see how far apart they are) is to create a DateTime object (with any date string) and then call getTimestamp(), and subtract the timestamps: <?php // when no string is given, it defaults to the current datetime (now).$date1 = (new DateTime)->getTimestamp();
$date2 = (new DateTime("2014-09-28 20:17:05"))->getTimestamp(); //this is possible because timestamps are just numbers$difference = ($date1 -$date2);

if($difference > 0) echo "date1 is larger (more recent)"; elseif($difference < 0)
echo "date2 is larger";
else
echo "both dates represent the same time (down to seconds)";


References
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.24308311939239502, "perplexity": 8577.04982768896}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-49/segments/1669446710698.62/warc/CC-MAIN-20221129132340-20221129162340-00495.warc.gz"}