Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple date formats #29

Closed
DenverCoder1 opened this issue Feb 16, 2021 · 20 comments · Fixed by #133
Closed

Support for multiple date formats #29

DenverCoder1 opened this issue Feb 16, 2021 · 20 comments · Fixed by #133
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@DenverCoder1
Copy link
Owner

Currently, all dates are displayed with month, day, and when applicable, year. For example, "Feb 16" or "Dec 31, 2020".

It may be possible to allow the user to optionally choose their preferred date format through a URL parameter. For example, if the user prefers the day before the month, they can display dates that way.

@DenverCoder1 DenverCoder1 added the enhancement New feature or request label Feb 16, 2021
@DenverCoder1 DenverCoder1 added the good first issue Good for newcomers label Mar 17, 2021
@vikasganiga05
Copy link
Contributor

vikasganiga05 commented Apr 10, 2021

Hi, I like to work on this, please help me where to get started?

Multiple Date Formats

image

@DenverCoder1
Copy link
Owner Author

DenverCoder1 commented Apr 10, 2021

Hi, @vikasganiga05! That would be great!

The function that would need to be updated is formatDate() in card.php.

In the function, it should check a URL parameter, for example $_REQUEST["date_format"] and that should tell it which format to display dates in.

The date can be converted to a string using PHP's format function as is already done.

If no date format is specified, the default formatting should be used.

It might make sense to tell the user to use format characters from PHP in the URL parameter. This would make the function simpler and allow full customization.

It would be nice to also have an option to hide the year when the year is the current year like the default currently does (basically remove the year -- which can be o, Y, or y and possibly spaces or date separators around it)

If you have any further questions, feel free to ask!

@vikasganiga05
Copy link
Contributor

Hi, @vikasganiga05! That would be great!

The function that would need to be updated is formatDate() in card.php.

In the function, it should check a URL parameter, for example $_REQUEST["date_format"] and that should tell it which format to display dates in.

The date can be converted to a string using PHP's format function as is already done.

If no date format is specified, the default formatting should be used.

It might make sense to tell the user to use format characters from PHP in the URL parameter. This would make the function simpler and allow full customization.

It would be nice to also have an option to hide the year when the year is the current year like the default currently does (basically remove the year -- which can be o, Y, or y and possibly spaces or date separators around it)

If you have any further questions, feel free to ask!

How the user provides the date format as a URL parameter, I mean the format of URL paremeter.

@DenverCoder1
Copy link
Owner Author

DenverCoder1 commented Apr 14, 2021

@vikasganiga05

I'm open to new ideas, but my current thought is something like this:

Parameter: date_format

Parameter: hide_year

  • Whether to hide the year when it is in the current year
  • true or false

Examples

?user=DenverCoder1&date_format=d%20F,%20Y

d%20F,%20Y gets decoded to d F, Y

"2020-04-14" will become "14 April, 2020"

"2021-04-14" will become "14 April"

?user=DenverCoder1&date_format=m/d/y&hide_year=false

"2020-04-14" will become "04/14/20"

"2021-04-14" will become "04/14/21"


Alternatively, there could be 2 date format parameters (eg. date_format and date_format_same_year) where the first is how to display dates when the year is not the current year and the second is how to display dates in the current year.

This would probably be easier to implement and allow people to customize the dates better.

@DenverCoder1
Copy link
Owner Author

@vikasganiga05

One more idea could be to combine them into one parameter by having brackets to represent the part to skip during the current year (See examples below).

The default value would be M j[, Y] in order to not change the existing behavior.

Let me know what you think.

Examples:

Input Result
date_format=d F[, Y]
"2020-04-14" => "14 April, 2020"

"2021-04-14" => "14 April"
date_format=j/n/Y
"2020-04-14" => "14/4/2020"

"2021-04-14" => "14/4/2021"
date_format=m/d[/y]
"2020-04-14" => "04/14/20"

"2021-04-14" => "04/14"
date_format=M j[, Y]
"2020-04-14" => "Apr 14, 2020"

"2021-04-14" => "Apr 14"

@vikasganiga05
Copy link
Contributor

@vikasganiga05

One more idea could be to combine them into one parameter by having brackets to represent the part to skip during the current year (See examples below).

The default value would be M j[, Y] in order to not change the existing behavior.

Let me know what you think.

Examples:

Input Result
date_format=d F[, Y]
"2020-04-14" => "14 April, 2020"

"2021-04-14" => "14 April"
date_format=j/n/Y
"2020-04-14" => "14/4/2020"

"2021-04-14" => "14/4/2021"
date_format=m/d[/y]
"2020-04-14" => "04/14/20"

"2021-04-14" => "04/14"
date_format=M j[, Y]
"2020-04-14" => "Apr 14, 2020"

"2021-04-14" => "Apr 14"

This idea sounds good, I will work on this.

@vikasganiga05
Copy link
Contributor

Hi sir, I found a way to implement the multiple date formats. But I don't know where to start. Please guide me 😞

image

@DenverCoder1
Copy link
Owner Author

@vikasganiga05 That's a good start.

In the end, we will need two strings, one to go into the call to date_format when the year matches and one for if the year doesn't match.

The date_format will need to be retrieved from a parameter passed to the function, for example, $format. That way the function can more easily be tested with different values.

Every time the formatDate function is called, the additional parameter will have to be passed. So those will need to be updated. In most cases, you will want to get the "date_format" parameter from the $params array or $_REQUEST if it is null. A variable assignment to $params ?? $_REQUEST can be added so that you don't have to use the whole thing every time.

Your code looks good, I would probably do it without the if since it will work whether the match is found or not, and preg_replace can be used for removing the part in brackets.

$differentYear = str_replace(array("[", "]"), "", $format);
$sameYear = preg_replace("/\[.*?\]/", "", $format);

If you have more questions, don't hesitate to ask! 🤗

@vikasganiga05
Copy link
Contributor

vikasganiga05 commented Apr 25, 2021

Ok, So I want to create a new dropdown for Date Formates. The default value will be M j[, Y] right?

@vikasganiga05
Copy link
Contributor

vikasganiga05 commented Apr 25, 2021

When I run the composer test, both the same year & different year tests passed. The below function looks good or anything to modify?

function formatDate(string $dateString, string $format): string
{
    $date = new DateTime($dateString);
    $isSameYear = preg_replace("/\[.*?\]/", "", $format);
    $sameYear = preg_replace("/\[.*?\]/", "", $format);
    $differentYear = str_replace(array("[", "]"), "", $format);
    // if current year, display only month and day
    if (date_format($date, "Y") == date("Y") && $isSameYear) {
        return date_format($date, $sameYear);
    }
    // otherwise, display month, day, and year
    return date_format($date, $differentYear);
}

@DenverCoder1
Copy link
Owner Author

Since $isSameYear seems to have the same value as $sameYear you could probably just use one of them.

Also, it would be great if you could add assertions to the tests that verify that the new feature will work in all cases.

Feel free to open a PR and I can leave comments there.

@vikasganiga05
Copy link
Contributor

Since $isSameYear seems to have the same value as $sameYear you could probably just use one of them.

Also, it would be great if you could add assertions to the tests that verify that the new feature will work in all cases.

Feel free to open a PR and I can leave comments there.

Sorry, my bad I forgot to change. It will be $isSameYear = preg_match("/\[.*?\]/", $format);

@DenverCoder1
Copy link
Owner Author

Sorry, my bad I forgot to change. It will be $isSameYear = preg_match("/\[.*?\]/", $format);

It seems to me that checking preg_match is unnecessary since whether there are brackets or not, preg_replace will give the correct result (it will not replace anything if the pattern is not found).

@vikasganiga05
Copy link
Contributor

Sorry, my bad I forgot to change. It will be $isSameYear = preg_match("/\[.*?\]/", $format);

It seems to me that checking preg_match is unnecessary since whether there are brackets or not, preg_replace will give the correct result (it will not replace anything if the pattern is not found).

Ok, I will remove it. The If condition look good? if (date_format($date, "Y") == date("Y") && $isSameYear) {

@DenverCoder1
Copy link
Owner Author

I think if (date_format($date, "Y") == date("Y")) { ... } should be enough.

@DenverCoder1
Copy link
Owner Author

Hi @vikasganiga05, any new progress on this?

@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2021

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the no-issue-activity No activity in 30+ days label Aug 8, 2021
@DenverCoder1
Copy link
Owner Author

Hi @vikasganiga05, it looks like it should be close to being ready, do you plan to finish this?

@DenverCoder1 DenverCoder1 reopened this Aug 14, 2021
@DenverCoder1 DenverCoder1 removed the no-issue-activity No activity in 30+ days label Aug 14, 2021
@DenverCoder1
Copy link
Owner Author

Since this has been open since February, I will unassign this issue in 7 days if there is no response.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 1, 2021

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the no-issue-activity No activity in 30+ days label Oct 1, 2021
@DenverCoder1 DenverCoder1 removed the no-issue-activity No activity in 30+ days label Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants