CutCodeDown

Web Development => PHP => Topic started by: GrumpyYoungMan on 8 Jan 2023, 05:03:30 am

Title: Brain Failure PHP date error not working sometimes...
Post by: GrumpyYoungMan on 8 Jan 2023, 05:03:30 am
The output produced:
Quote
[convertDate] Raw (Step 1): 21/03/2023
[formatDate] Raw (Step 2): 1970-01-01T12:00:00 (Mode: date)
January 1st, 1970

=======

[convertDate] Raw (Step 1): 10/05/2023
[formatDate] Raw (Step 2): 2023-10-05T12:00:00 (Mode: date)
October 5th, 2023

The Code:
Code: [Select]
function convertDate($timestamp) {
    echo '<div>[', __FUNCTION__, '] Raw (Step 1): ', $timestamp, '</div>';
    return formatDate(
        date(
            "Y-m-d\Th:i:s",
            strtotime(
                str_replace(
                    '-"',
                    '/',
                    $timestamp
                )
            )
        ),
        "date"
    );
}
function formatDate($timestamp, $mode) {
    echo '<div>[', __FUNCTION__, '] Raw (Step 2): ', $timestamp, ' (Mode: ',$mode,')</div>';
    list($date,$time) = explode("T", $timestamp);

    $date = date(
        "F jS, Y",
        strtotime($date)
    );

    if (
        $mode == "date"
    )
        return $date;
    else
        return;

}

What have I missed? Please?
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: GrumpyYoungMan on 8 Jan 2023, 10:49:33 am
I think the issue is that it is getting the month and day of month confused. Will break it down and try and isolate the issue but suspect it’s a strtotime problem.
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: GrumpyYoungMan on 8 Jan 2023, 05:38:21 pm
I have resolved this issue, but will post the modify code tomorrow.

Basically it was the date format being sent to strtotime causing the problem. (It also looks like I don’t need to replace /‘s with -‘s…)
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: GrumpyYoungMan on 9 Jan 2023, 03:32:04 am
Modified code, which seems to work:
Code: [Select]
function convertDate($timestamp, $mode) {
    return formatDate(
                date(
            "d-m-Y\Th:i:s",
                        strtotime(
                            str_replace(
                            '/',
                            '-',
                            $timestamp
                            )
                        )
                    ),
            $mode
            );
}
function formatDate($timestamp, $mode) {
    list(
        $date,
        $time
    ) =
        explode(
                "T",
                $timestamp
        );

    $date = date (
            "F jS, Y",
            strtotime (
                    $date
            )
    );

    if (
        $mode == "date"
    )
        return $date;
    else
        return "-";

Which will output
Quote
Raw: 10/05/2023 Formatted: May 10th, 2023
Raw: 21/03/2023 Formatted: March 21st, 2023

strtotime (https://www.php.net/manual/en/function.strtotime.php) doesn't seem to like m-d-Y.
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: Jason Knight on 9 Jan 2023, 07:09:00 am
My question would be why do you have something called $timestamp that isn't actually a timestamp? (utc integer)

Only time you should be formatting to a simple format is for output, not processing.

Though honestly this is why I NEVER use a numeric month... for anything. Same reason the US military doesn't. EVER.
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: GrumpyYoungMan on 9 Jan 2023, 08:56:29 am
My question would be why do you have something called $timestamp that isn't actually a timestamp? (utc integer)

Only time you should be formatting to a simple format is for output, not processing.

Though honestly this is why I NEVER use a numeric month... for anything. Same reason the US military doesn't. EVER.
Sadly, the API sends the date back like that and in a mismatch of ways too.... I have to work with what the API sends back to me... 
Title: Re: Brain Failure PHP date error not working sometimes...
Post by: Jason Knight on 9 Jan 2023, 06:57:57 pm
Whereas I'd use that alone as an indication the API was built by some know-nothing turd and thus not worth using or getting even remotely involved in using.

I've spent 30+ years walking away from "problems" like that, it's never worth the effort. Something as simple as not using a clear date just means it's likely everything else shows that same lack of foresight and basic competence.