Welcome Guest, Not a member yet? Register   Sign In
having problem printing out "created_at" date
#1

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){
foreach (
$menu_list as $item) {
echo 
$item->name;
echo 
$item->created_at;
echo 
$item->updated_at;



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  Confused
Reply
#2

(This post was last modified: 09-05-2021, 12:12 PM by ikesela.)

if not sure , u can cast $item to array

$items = $item->toArray();
Reply
#3

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();
$query $db->table('player_genres')
->
where('name_id'48)
->
get();

foreach ( 
$query->getResult() as $row ) {
    echo "$row->created_at ==> $row->updated_at<br>";
}

echo 
"<br><pre>";
print_r ($row);
echo 
"</pre>"
The result was:
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
)
Reply
#4

$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:mmConfuseds [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:mmConfuseds [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.
Reply
#5

@chakycool can you print_r the value of $item->created_at?
PHP Code:
print_r($item->created_at); 
Reply
#6

(This post was last modified: 09-06-2021, 04:10 AM by ikesela.)

(09-05-2021, 11:44 PM)chakycool Wrote: $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:mmConfuseds [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:mmConfuseds [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.

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 =
Reply
#7

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');
echo 
$item->updated_at->format('d-M-Y H:i:s'); 

You can change the date format by supplying in the argument of format().
Reply
#8

(This post was last modified: 09-06-2021, 06:11 AM by chakycool.)

Thanks paulbalanda, it worked.

One more thing, how can I use the "humanize()" on this date?
Reply
#9

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();
// will return something like "2 hours ago" 
Reply
#10

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();
Reply




Theme © iAndrew 2016 - Forum software by © MyBB