CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Brain Failure PHP date error not working sometimes...  (Read 330 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Brain Failure PHP date error not working sometimes...
« 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?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Brain Failure PHP date error not working sometimes...
« Reply #1 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.
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Brain Failure PHP date error not working sometimes...
« Reply #2 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…)
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Brain Failure PHP date error not working sometimes...
« Reply #3 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 doesn't seem to like m-d-Y.
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1049
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Brain Failure PHP date error not working sometimes...
« Reply #4 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.
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Brain Failure PHP date error not working sometimes...
« Reply #5 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... 
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 1049
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Brain Failure PHP date error not working sometimes...
« Reply #6 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.
We are all, we are all, we are all FRIENDS! For today we're all brothers, tonight we're all friends. Our moment of peace in a war that never ends.

 

SMF spam blocked by CleanTalk

Advertisement