Have my API returning diffForHumans() on my created at date:
public function getCreatedAtAttribute($value)
{
return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value)->diffForHumans();
}
This creates e.g.
1 day ago
6 days ago
3 weeks ago
1 month ago
But What I wish for is to have the same as Instagram where it returns simply:
1d
6d
3w
1m
Is there a way to do this apart from perhaps splitting the returned value by spaces and taking the first letter of the second word and the number?
EDIT: One of those lovely occasions where typing out a question solves it for you.
This is what I just came up with quick. Seems to work but open to any other solutions.
public function getCreatedAtAttribute($value)
{
$diffForHumans = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value)->diffForHumans();
$data = explode(" ", $diffForHumans);
$number = $data[0];
$denominator = substr($data[1], 0, 1);
return $data[0] . $denominator;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire