Added some helpers for date string formatting.

This commit is contained in:
Lukas Halbritter
2018-10-01 17:17:41 +02:00
parent e9077193af
commit cc203f36c7
+43 -13
View File
@@ -4,10 +4,10 @@
* Returns true with a given percentage.
*
* @param int $prob Probability for true in percent.
*
* @return boolean
*/
function rand_bool($prob = 50) {
function rand_bool($prob = 50)
{
if ($prob < 0 or $prob > 100) {
throw new InvalidArgumentException('The value for the probability '.
'must be in a range from 0 to 100.');
@@ -28,35 +28,65 @@ function rand_bool($prob = 50) {
* argument.
*
* @param integer $decimals Sets the number of decimal points.
*
* @return string
*/
function formatAmount(float $amount, $decimals = 2) {
return number_format($amount, $decimals, ',', '.');
function formatAmount(float $amount, $cash = true, $text = false,
$decimals = 2)
{
$euro = ($cash) ? ' €' : '';
if (!$text) {
$sign = ($amount < 0) ? '- ' : '';
$left = '<span style=\'float: left;\'>';
$right = '<span style=\'float: right;\'>';
$span = '</span>';
if ($amount == 0) {
return $right . '-&nbsp;' . $euro . $span;
}
return $left . $sign . $span . $right .
number_format(abs($amount), $decimals, ',', '.') . $euro . $span;
}
return number_format(abs($amount), $decimals, ',', '.') . $euro;
}
/**
* Return a formatted date string.
*
* @param string $date
* @param string $format
* @return string
*/
function formatDate($date, $format='d.m.Y')
{
return Carbon\Carbon::parse($date)->format($format);
}
/**
* Return the string that is the first day of the month, given by the year and
* the month.
*
* @param string $year
* @param string $month
* @param string $year
* @param string $month
* @return string
*/
function startOfMonth($year, $month) {
function startOfMonth($year, $month, $format='d.m.Y')
{
return Carbon\Carbon::createFromDate($year, $month)
->startOfMonth()->format('Y-m-d');
->startOfMonth()->format($format);
}
/**
* Return the string that is the last day of the month, given by the year and
* the month.
*
* @param string $year
* @param string $month
* @param string $year
* @param string $month
* @return string
*/
function endOfMonth($year, $month) {
function endOfMonth($year, $month, $format='d.m.Y') {
return Carbon\Carbon::createFromDate($year, $month)
->endOfMonth()->format('Y-m-d');
->endOfMonth()->format($format);
}