Skip to content

Instantly share code, notes, and snippets.

@jokamjohn
Last active October 1, 2015 20:09
Show Gist options
  • Save jokamjohn/c4dea038bec9dd28c355 to your computer and use it in GitHub Desktop.
Save jokamjohn/c4dea038bec9dd28c355 to your computer and use it in GitHub Desktop.
Php Carbon Extension for the Date.....Some of the cool methods
<?php
require 'vendor/autoload.php';
/**
* Created By John Kagga
* Some of the carbon time methods
* Play around with them
*
**/
use Carbon\Carbon;
//kampala time
echo Carbon::now('Africa/Kampala');
echo '<br>';
//tomorrow
echo Carbon::now()->addDay();
echo '<br>';
echo '<br>';
//tomorrow
echo Carbon::now()->tomorrow();
echo '<br>';
//yesterday
echo Carbon::now()->yesterday();
echo '<br>';
//timestamp
$time = Carbon::now()->subYear('1')->timestamp;
echo $time;
echo '<br>';
//time from timestamp
$date = Carbon::createFromTimestamp($time);
//day from timestamp
echo $date->day;
echo '<br>';
//day of the week from timestamp
echo $date->dayOfWeek;
echo '<br>';
//month from the timestamp
echo $date->month;
echo '<br>';
//year from the timestamp
echo $date->year;
echo '<br>';
//time for humans
echo $date->diffForHumans();
echo '<br>';
//number of days ago
$newDate = Carbon::now()->addDays(4)->addMinutes(30);
echo $newDate->diffForHumans();
echo '<br>';
//first days of the month
$date = Carbon::now();
echo $date->startOfMonth();
echo '<br>';
echo 'Time:' . $date->diffForHumans();
echo '<br>';
//end of month
echo $date->endOfMonth();
echo '<br>';
echo 'Time:' . $date->diffForHumans();
echo '<br>';
//Formatting the dates
//now
echo Carbon::now()->toDateTimeString();
echo '<br>';
//Todays date in words
echo Carbon::now()->toFormattedDateString();
echo '<br>';
//Atom feed time
echo Carbon::now()->toATOMString();
echo '<br>';
//carbon instances
echo 'Next Monday' .$newDate = new Carbon('next Monday');
echo '<br>';
echo 'Day of Week' . $newDate->dayOfWeek;
echo '<br>';
echo 'Month' . $newDate->month;
echo '<br>';
echo $newDate->diffForHumans();
echo '<br>';
//playing around
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment