Skip to content

Instantly share code, notes, and snippets.

@phamngsinh
Created June 13, 2016 09:54
Show Gist options
  • Select an option

  • Save phamngsinh/45f7439b6e8210fcb24746238af735f1 to your computer and use it in GitHub Desktop.

Select an option

Save phamngsinh/45f7439b6e8210fcb24746238af735f1 to your computer and use it in GitHub Desktop.
Laravel, PHP Validate Datetime
$time = null;
if($timestamps instanceof DateTime){
$time = new \Carbon\Carbon($timestamps);
}
if($timestamps instanceof \Carbon\Carbon){
$time = $timestamps;
}
if (is_integer($timestamps) && strtotime(date($format, $timestamps)) === (int)$timestamps) {
$dateTime = date($format, $timestamps);
$time = new \Carbon\Carbon($dateTime);
}
if (is_string($timestamps) && strtotime($timestamps)) {
$dateTime = date($format, strtotime($timestamps));
$time = new \Carbon\Carbon($dateTime);
}
return $time;
@phamngsinh
Copy link
Copy Markdown
Author

    echo '1';print_r(convert_time_u_to_carbon(time()));echo '<br/>';
    echo '2';print_r(convert_time_u_to_carbon(0));echo '<br/>';
    echo '3';print_r(convert_time_u_to_carbon(1));echo '<br/>';
    echo '4';print_r(convert_time_u_to_carbon("2016-2-23"));echo '<br/>';
    echo '5';print_r(convert_time_u_to_carbon("2016-23-3"));echo '<br/>';//dfdf
    echo '6';print_r(convert_time_u_to_carbon(-1));echo '<br/>';//dfdf
    echo '7';print_r(convert_time_u_to_carbon('asdfadfasd'));echo '<br/>';//dfdf
    echo '8';print_r(convert_time_u_to_carbon("2016223"));echo '<br/>';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment