date conversion

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
wilsoc31
New php-forum User
New php-forum User
Posts: 2
Joined: Tue May 25, 2021 5:58 pm

Tue May 25, 2021 6:11 pm

Can someone please tell me how the converted date stored on the database of:
1608754373

is converted to:
Jan 24, 2021

I can find things that get close but i always seem to be 2 or more days off
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 421
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Wed May 26, 2021 5:32 am

Code: Select all

/*
 * First you have the incorrect timestamp for Jan 24, 2021
 */
$myDate = new DateTime('Jan 24, 2021', new DateTimeZone("America/Detroit"));
echo $myDate->format('U = Y-m-d H:i:s') . "<br>\n"; // Timestamp and MySQL Date Format

/*
 * This is how you set the timestamp using the DateTime() method/function:
 */
$myDate->setTimestamp(1611464400);

echo "Date " . $myDate->format("F j, Y") . "<br>"; // Standard Date Format
Life is a fig newton of your imagination! https://www.phototechguru.com/
wilsoc31
New php-forum User
New php-forum User
Posts: 2
Joined: Tue May 25, 2021 5:58 pm

Wed May 26, 2021 5:58 am

Strider,
Thank you for the reply. When a post on the forum has been submitted.. it stores the date in the table as
1608754373

When logging on the forum and looking via browser it says the post was created
Jan 24, 2021


So I'm asking, how do I get those numbers converted to a date format that matches back to 24-JAN-2021
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 137
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed May 26, 2021 10:02 am

The number you have given is a Unix timestamp; the number of seconds since the first of January 1970. It maps to the 23rd December 2020; not the date you've given. Are you sure that's where the date comes from?
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 421
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Wed May 26, 2021 1:28 pm

Code: Select all

<?php
/*
 * First you have the incorrect timestamp for Jan 24, 2021
 */
try {
    $myDate = new DateTime('Jan 24, 2021', new DateTimeZone("America/Detroit"));
} catch (Exception $e) {
}
echo $myDate->format('U = Y-m-d H:i:s') . "<br>\n"; // Timestamp and MySQL Date Format

/*
 * This is how you set the timestamp using the DateTime() method/function:
 */
$myDate->setTimestamp(1611464400);

echo "Date " . $myDate->format("F j, Y") . "<br>"; // Standard Date Format

/* I sure don't know how you are getting timestamp for the date */
$date = new DateTime(' 24-JAN-2021');
echo date_timestamp_get($date) . "<br>";
echo $date->format("j-M-Y") . "<br>";
I sure don't know how you are getting that date for that timestamp, but it's possible the timestamp is being converted in MySQL itself - https://dev.mysql.com/doc/refman/8.0/en/datetime.html. I personally don't mess around too much with MySQL as I like doing it in PHP with DateTime and rarely play around with timestamp.
Life is a fig newton of your imagination! https://www.phototechguru.com/
Post Reply