Skip to content
On this page

Leaf Date

Leaf Date makes formating date/time really easier, though the method names may appear weird, they're actually very easy to remeber😂

use Leaf\Date;

Date::now();

Installation

You can install leaf date through composer:

composer require leafs/date

or with leaf cli:

leaf install date

Date Methods

Basically, Date just gives you methods to manipulate the date and time, faster and simpler than DateTime() would allow. Let's take a look at these methods.

randomTimestamp

randomTimestamp as the name implies, generates a new random timestamp. This method was Timestamp in the previous version.

$timestamp = Date::randomTimestamp();

randomTimestamp is already configured to give a wide range of dates, but if you ever feel like customizing this range, just pass in params for randomTimestamp

Date::randomTimestamp($start, $end);

randomDate

random_date as the name implies, generates a new random date.

$timestamp = Date::randomDate();

Just like randomTimestamp, randomDate is already configured with a wide range of dates, but you can set a specific range with:

Date::randomDate($start, $end);

getTimezone

This method returns the current time zone of the user

$timeZone = Date::getTimezone();

setTimezone

This method sets the timezone sets the timezone of the user. This method takes in one optional parameter, which is the timezone to set. If nothing is passed in there, the timezone is set to a GMT timezone.

$timeZone = Date::setTimezone('Timezone');

You can get a list of all PHP supported timezones here

rawDate

Format a local time/date

$date = Date::rawDate(time(), "D, d M Y H:i:s");

$date = Date::rawDate(time());

now

now returns the timestamp of the current date and time. Now will return the date based on the timezone, therefore, it is recommended to use now together with setTimeZone

Date::setTimeZone('Africa/Accra');
$timezone = Date::now();

daysAgo

This is used to get the date from some days ago.

$now = Date::now(); // returns 2020-04-15 05:21:43 pm
Date::daysAgo(2); // returns 2020-04-13

You can also pass in a particular date as a second parameter. This will be the date to start counting from.

echo Date::daysAgo(10, "2020-04-13"); // returns 2020-04-03

monthsAgo

This is used to get the date from some months ago.

$now = Date::now(); // returns 2020-04-15 05:21:43 pm
Date::monthsAgo(2); // returns 2020-02-15

You can also pass in a particular date as a second parameter. This will be the date to start counting from.

echo Date::monthsAgo(10, "2020-04-13"); // returns 2019-06-13

yearsAgo

This is used to get the date from some years ago.

$now = Date::now(); // returns 2020-04-15 05:21:43 pm
Date::yearsAgo(2); // returns 2018-04-15

You can also pass in a particular date as a second parameter. This will be the date to start counting from.

echo Date::yearsAgo(10, "2020-04-13"); // returns 2010-04-13

toDate

This method gets the date in YYYY-MM-DD format from an existing timestamp

$parsedDate = Date::toDate($timestamp);

toEnglishDate

This gets the date in the format (MM DD, YYYY) from an existing timestamp

$parsedDate = Date::toEnglishDate($timestamp);

toEnglishTs

This gets the date in the format (DD MM, YYYY HH:MM:SS) from a timestamp

$parsedDate = Date::toEnglishTs($timestamp);

toTime

This gets the time in the format (HH:MM:SS) from a timestamp

$parsedDate = Date::toTime($timestamp);

format

Format a timestamp based on your own rules

$parsedDate = Date::format($timestamp, "d-m-Y");

intToMonth

This gets the month in words from a number (1-12)

$month = Date::intToMonth($number);

intToDay

This gets the day in words from a number (1-7)

$month = Date::intToDay($number);

day

Get current day

$date = Date::day();

month

Get current month

$month = Date::month();

year

Get current year

$year = Date::year();
Leaf Date has loaded