having problem printing out "created_at" date |
This sound silly but I don't know what I'm doing wrong I just cant seem to echo out the "created_at" & "updated_at" fields coming from the DB. Times stamps are created automatically using "protected $useTimestamps = true"
This is the loop PHP Code: if ($menu_list != null){ Only the Name is printed. Now if I do print a single loop data on the array I get the below PHP Code: print_r($item); App\Entities\Admin\Menu\MenuData Object ( [datamap:protected] => Array ( ) [dates:protected] => Array ( [0] => created_at [1] => updated_at [2] => deleted_at ) [casts:protected] => Array ( ) [castHandlers:protected] => Array ( ) [defaultCastHandlers:CodeIgniter\Entity\Entity:private] => Array ( [array] => CodeIgniter\Entity\Cast\ArrayCast [bool] => CodeIgniter\Entity\Cast\BooleanCast [boolean] => CodeIgniter\Entity\Cast\BooleanCast [csv] => CodeIgniter\Entity\Cast\CSVCast [datetime] => CodeIgniter\Entity\Cast\DatetimeCast [double] => CodeIgniter\Entity\Cast\FloatCast [float] => CodeIgniter\Entity\Cast\FloatCast [int] => CodeIgniter\Entity\Cast\IntegerCast [integer] => CodeIgniter\Entity\Cast\IntegerCast [json] => CodeIgniter\Entity\Cast\JsonCast [object] => CodeIgniter\Entity\Cast\ObjectCast [string] => CodeIgniter\Entity\Cast\StringCast [timestamp] => CodeIgniter\Entity\Cast\TimestampCast [uri] => CodeIgniter\Entity\Cast\URICast ) [attributes:protected] => Array ( [menu_id] => 26 [comp_id] => 20002 [name] => Appetizers [image] => 20210905162616247.jpg [edited_by] => 0 [created_at] => 2021-09-05 16:25:34 [updated_at] => 2021-09-05 16:26:17 ) [original:protected] => Array ( [menu_id] => 26 [comp_id] => 20002 [name] => Appetizers [image] => 20210905162616247.jpg [edited_by] => 0 [created_at] => 2021-09-05 16:25:34 [updated_at] => 2021-09-05 16:26:17 ) [_cast:CodeIgniter\Entity\Entity:private] => 1 ) So I can see the data, but why can't it print
I don't use entities, just query->builder and I never had the need to print "created_at" and "updated_at" but I decided to do a test and it worked fine:
PHP Code: $db = db_connect(); 2021-09-02 12:09:10 ==> 2021-09-02 12:10:24 stdClass Object ( [id] => 88 [name_id] => 48 [genre] => Easy Listening [created_at] => 2021-09-02 12:09:10 [updated_at] => 2021-09-02 12:10:24 )
$item to array didn't make things better.
Array ( [menu_id] => 26 [comp_id] => 20002 [name] => Appetizers [image] => 20210905162616247.jpg [edited_by] => 0 [created_at] => CodeIgniter\I18n\Time Object ( [timezone:protected] => DateTimeZone Object ( [timezone_type] => 3 [timezone] => xx/xx) [locale:protected] => en-ae [toStringFormat:protected] => yyyy-MM-dd HH:mms [date] => 2021-09-05 16:25:34.000000 [timezone_type] => 3 [timezone] => xx/xx) [updated_at] => CodeIgniter\I18n\Time Object ( [timezone:protected] => DateTimeZone Object ( [timezone_type] => 3 [timezone] => xx/xx) [locale:protected] => en-ae [toStringFormat:protected] => yyyy-MM-dd HH:mms [date] => 2021-09-05 16:26:17.000000 [timezone_type] => 3 [timezone] => xx/xx ) ) Thank you guys for the recommendations but I want to do this is the new CI4 way with entities. Hope I get a CI4 way to solve this.
(09-05-2021, 11:44 PM)chakycool Wrote: $item to array didn't make things better. Its not CI4 , but your entities that cast time to I18n\Time object. so $item->created_at must be an instance of CodeIgniter\I18n\Time() check your entiti at this variable: protected $casts =
It seems when using Entity, the dates are returned as Time instances. So you're actually echoing an object. Do this instead:
PHP Code: echo $item->created_at->format('d-M-Y H:i:s'); You can change the date format by supplying in the argument of format().
I haven't used humanize before but since it is a method of Time class you can call that directly on your dates.
PHP Code: echo $item->created_at->humanize();
Tried this and I get a error " Call to a member function getTime() on null"
SYSTEMPATH\I18n\Time.php at line 1168 * 1162 * @return mixed 1163 * @throws Exception 1164 */ 1165 public function humanize() 1166 { 1167 $now = IntlCalendar::fromDateTime(Time::now($this->timezone)->toDateTimeString()); 1168 $time = $this->getCalendar()->getTime(); |
Welcome Guest, Not a member yet? Register Sign In |