Leaf Date makes formating date/time really easier, though the method names may appear weird, they're actually very easy to remeber😂.
$date = new Leaf\Date();New in v2 The Date object has been directly bound to the Leaf object, so we can use it without initialising it.
$leaf = new Leaf\App();
$leaf->date;After this, all the Date methods will be available to us:
NOTE that in v2, all method names now use snake_case instead of the previous camelCase
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.
random_timestamp as the name implies, generates a new random timestamp. This method was Timestamp in the previous version.
$timestamp = $date->random_timestamp();random_timestamp is already configured to give a wide range of dates, but if you ever feel like customizing this range, just pass in params for random_timestamp
$date->random_timestamp($start, $end);random_date as the name implies, generates a new random date.
$timestamp = $date->random_date();Just like randomTimestamp, random_date is already configured with a wide range of dates, but you can set a specific range with:
$date->random_date($start, $end);This method returns the current time zone of the user
$timeZone = $date->get_timezone();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->set_timezone('Timezone');You can get a list of all PHP supported timezones here
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->set_timezone('Africa/Accra');
$timezone = $date->now();This is used to get the date from some days ago.
$now = $date->now(); // returns 2020-04-15 05:21:43 pm
$date->days_ago(2); // returns 2020-04-13You can also pass in a particular date as a second parameter. This will be the date to start counting from.
echo $date->days_ago(10, "2020-04-13"); // returns 2020-04-03This is used to get the date from some months ago.
$now = $date->now(); // returns 2020-04-15 05:21:43 pm
$date->months_ago(2); // returns 2020-02-15You can also pass in a particular date as a second parameter. This will be the date to start counting from.
echo $date->months_ago(10, "2020-04-13"); // returns 2019-06-13This is used to get the date from some years ago.
$now = $date->now(); // returns 2020-04-15 05:21:43 pm
$date->years_ago(2); // returns 2018-04-15You can also pass in a particular date as a second parameter. This will be the date to start counting from.
echo $date->years_ago(10, "2020-04-13"); // returns 2010-04-13Previously GetDateFromTimeStamp
This method gets the date in YYYY-MM-DD format from an existing timestamp
$parsedDate = $date->ts_to_date($timestamp);Previously GetEnglishDateFromTimeStamp
This gets the date in the format (MM DD, YYYY) from an existing timestamp
$parsedDate = $date->ts_to_english_date($timestamp);Previously GetEnglishTimeStampFromTimeStamp
This gets the date in the format (DD MM, YYYY HH:MM:SS) from a timestamp
$parsedDate = $date->ts_to_english_ts($timestamp);Previously GetTimeFromTimeStamp
This gets the time in the format (HH:MM:SS) from a timestamp
$parsedDate = $date->ts_to_time($timestamp);Format a timestamp based on your own rules
$parsedDate = $date->format($timestamp, "d-m-Y");Previously GetMonthFromNumber
This gets the month in words from a number (1-12)
$month = $date->int_to_month($number);Previously GetDayFromNumber
This gets the day in words from a number (1-7)
$month = $date->int_to_day($number);Get current day
$date = $date->day();Get current month
$month = $date->month();Get current year
$year = $date->year();Request Response Session Environment Using a database
Built with ❤ by Mychi Darko