Jump to content

Find the difference between two dates in non-standard format, mysql


Go to solution Solved by requinix,

Recommended Posts

col1 col2

28/Apr/24 11:50 AM 29/Apr/24 5:29 PM

This is honestly an interesting sql project of mine(I can use node.js as well).

This is how I'd solve this manually:

29/Apr/24

Will be converted to

29/Apr/24

And it'll be converted to

29/04/24 ( There'll be a lookup table for Apr->04 mapping)

And the time will be converted 17:29 (no unit PM or AM)

Then,

I'll convert the col1 date to the similar format.

28/04/24

Then time to same format 24h format.

And subtract it.

But I can't make sql to do this as I've not much idea about SQL syntax. Can anyone help me? my alternative is using node.js

Step 1: stop storing them in a non-standard format and use DATETIMEs instead.

There is no step 2. Seriously. Use a standard format and this problem, and very likely other problems you've had or are yet to have, will go away.

  • Solution
Posted (edited)

...What are you using to view the CSV file?

edit: Nevermind, looks like it does use a localized format after all. Stupid Jira.

For PHP, use date_parse_from_format() or DateTime's createFromFormat, using the "d/M/y h:i a" format.
https://3v4l.org/NPZvF

Edited by requinix

Some example code for you

// INPUT DATES
$col1 = '28/Apr/24 11:50 AM';
$col2 = '29/Apr/24 5:29 PM';
// FORMAT DEFINITIONS
$jira = 'd/M/y h:i a';
$SQLformat = 'Y-m-d H:i:s';

$dt1 = DateTime::createFromFormat($jira, $col1);             // create DateTime object
$dt2 = DateTime::createFromFormat($jira, $col2);             
$d = $dt1->diff($dt2);                                       // create DateInterval object

echo $dt1->format($SQLformat) .'<br>';                       // output dates
echo $dt2->format($SQLformat) .'<br>';
printf("%d days %d hr %d min", $d->days, $d->h, $d->i);    // show difference

Output...

2024-04-28 11:50:00
2024-04-29 17:29:00
1 days 5 hr 39 min

 

  • Like 1

You can do it SQL when you load the CSV data into MySQL using the STR_TO_DATE() function to reformat the dates

// CSV INPUT
$input = 'Tom DiCanari,28/Apr/24 11:50 AM,29/Apr/24 5:29 PM';
// create tete table
$pdo->exec("CREATE TABLE IF NOT EXISTS test1 (name varchar(50), timefrom datetime, timeuntil datetime)");

// process the input
$stmt = $pdo->prepare("INSERT INTO test1 (name, timefrom, timeuntil) 
                       VALUES (?, STR_TO_DATE(?, '%d/%b/%y %h:%i %p'), STR_TO_DATE(?, '%d/%b/%y %h:%i %p'))");

$data = str_getcsv($input);
$stmt->execute($data);

then

mysql> SELECT name
    ->      , timefrom
    ->      , timeuntil
    ->      , timestampdiff(MINUTE, timefrom, timeuntil) as mins_diff
    -> FROM test1;
+--------------+---------------------+---------------------+-----------+
| name         | timefrom            | timeuntil           | mins_diff |
+--------------+---------------------+---------------------+-----------+
| Tom DiCanari | 2024-04-28 11:50:00 | 2024-04-29 17:29:00 |      1779 |
+--------------+---------------------+---------------------+-----------+

 

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.