Skip to content

Commit

Permalink
Add helpers and method on datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jan 21, 2024
1 parent b7ad2e4 commit 09282ee
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
32 changes: 32 additions & 0 deletions resources/Js/Handlebars/round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Handlebars Array Helpers
*
* @source https://github.com/helpers/handlebars-helpers
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @copyright 2022-2023 Kévin Zarshenas
*/

/**
* Length
*
* Returns the length of the given string or array.
*
* @param any value
* @param Object options
*
* @return number
*/
module.exports = (value, options) => {

// Check if array or string
if(typeof value === 'number')

// Return lenght
return Math.round(value);

// Return 0
return value;

}
33 changes: 33 additions & 0 deletions src/Front/Library/Utility/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,37 @@ export default class DateTime {

}

/**
* Get Next Day
*
* Return in format YYYY/MM/DD
*
* @param weekday
* @returns string
*/
public static getNextDay = (weekday:1|2|3|4|5|6|7) => {

// Check week day
if (weekday < 1 || weekday > 7)

// New error
throw new Error("Invalid weekday number. Please enter a number between 1 (Monday) and 7 (Sunday).");

// New date
const today = new Date();
const todayDayOfWeek = today.getUTCDay(); // Sunday - 0, Monday - 1, etc.
const daysUntilNext = (weekday - todayDayOfWeek + 7) % 7; // Calculate days until the next desired weekday

// Set the date to the next desired weekday
today.setUTCDate(today.getUTCDate() + daysUntilNext);

// Format the date as YYYY/MM/DD
const year = today.getUTCFullYear();
const month = (today.getUTCMonth() + 1).toString().padStart(2, '0'); // Months are zero-indexed
const day = today.getUTCDate().toString().padStart(2, '0');

// Return date
return `${year}/${month}/${day}`;
}

}
2 changes: 2 additions & 0 deletions src/Library/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ public static function downloadToTmp(string $url = "", string $tempFileName = "t
"jpeg" => "image/jpeg",
# Png
"png" => "image/png",
# WebP
"webp" => "image/webp",
# TBC ...
];

Expand Down
23 changes: 23 additions & 0 deletions src/Library/Template/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,27 @@ public static function length($value) {

}

/**
* Round
*
* Returns the length of the given string or array.
*
* @param mixed $value
* @param mixed options
*/
public static function round($value) {

# Check if array
if(is_int($value) || is_float($value) || ctype_digit($value))

# Return round value
return round(intval($value));

else

# Return value
return $value;

}

}

0 comments on commit 09282ee

Please sign in to comment.