Last active
November 26, 2020 11:52
-
-
Save jatubio/ba40b3537543e002222e to your computer and use it in GitHub Desktop.
Get random dates for created_at and updated_at fields on Laravel Framework using Carbon package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getRandomTimestamps($backwardDays = null) | |
{ | |
if ( is_null($backwardDays) ) | |
{ | |
$backwardDays = -800; | |
} | |
$backwardCreatedDays = rand($backwardDays, 0); | |
$backwardUpdatedDays = rand($backwardCreatedDays + 1, 0); | |
return [ | |
'created_at' => \Carbon\Carbon::now()->addDays($backwardCreatedDays)->addMinutes(rand(0, | |
60 * 23))->addSeconds(rand(0, 60)), | |
'updated_at' => \Carbon\Carbon::now()->addDays($backwardUpdatedDays)->addMinutes(rand(0, | |
60 * 23))->addSeconds(rand(0, 60)) | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool construction ))