CutCodeDown
Web Development => PHP => Topic started by: GrumpyYoungMan on 8 Jan 2023, 05:03:30 am
-
The output produced:
[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:
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?
-
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.
-
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…)
-
Modified code, which seems to work:
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
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.
-
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.
-
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...
-
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.