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: PHP - Date What format  (Read 1083 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
PHP - Date What format
« on: 23 Jul 2020, 11:13:23 am »
What is the best method for handling and storing date(s)

YYYY-MM-DD or Unix timestamps?

I used to use the Unix timestamps, but I was lead to believe I shouldn't now but it seems I have to always convert to the Unix time stamps when handling the date format?
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: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: PHP - Date What format
« Reply #1 on: 23 Jul 2020, 08:02:30 pm »
For what? What are you handling or storing them IN?

Much like with JavaScript the 'native format" you use shouldn't matter if you're manipulating them, as the first thing you should be doing is assigning it to a DateTime object which can do all your juggling and manipulation for you:

https://www.php.net/manual/en/class.datetime.php

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: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: PHP - Date What format
« Reply #2 on: 24 Jul 2020, 02:15:34 am »
I’ll have a look, Thanks Jason.

I was mainly using date for things like added, updated and anniversary dates in MySQL...?
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: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: PHP - Date What format
« Reply #3 on: 24 Jul 2020, 04:26:30 am »
For what? What are you handling or storing them IN?

Much like with JavaScript the 'native format" you use shouldn't matter if you're manipulating them, as the first thing you should be doing is assigning it to a DateTime object which can do all your juggling and manipulation for you:

https://www.php.net/manual/en/class.datetime.php
Does that also mean we should stop using date now?
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: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: PHP - Date What format
« Reply #4 on: 27 Jul 2020, 10:37:04 pm »
I was mainly using date for things like added, updated and anniversary dates in MySQL...?
Since MySQL has it's own date type, a lot of those -- like if you want "right now" -- you should be using NOW() in your SQL with a date field.

UPDATE someTable
updated = NOW()
WHERE id = ?

For example. Unless you're doing date math -- and even then -- dates aren't even your PHP's job when dealing with mysql apart from turning form input into something compatible with mysql dates, or turning the date from mysql into markup formatting... which is when the normal "date" function is fine and dandy.

But like anniversaries, you want to add a year, do it in the query. You want to pull something that happened on the same day and month, use DAY() and MONTH() or DAYOFMONTH() in the query.
« Last Edit: 27 Jul 2020, 10:39:30 pm by Jason Knight »
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: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: PHP - Date What format
« Reply #5 on: 28 Jul 2020, 02:01:17 am »
Thanks again Jason, I was already using NOW() for the insert and update date.

I wasn’t aware of the other date functions...

I will have a look and post some code samples for you to pass comment on... I’m sure it’ll most likely be bad but that’s all part of the learning process...
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: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: PHP - Date What format
« Reply #6 on: 29 Jul 2020, 04:53:08 am »
Code: [Select]
date("F dS Y", strtotime($r['expiry']));
So is the above code updated to:

Code: [Select]
date_format( date_create($r['date_added']), 'F dS Y');
when using PHP version 7 >
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: 1054
  • Karma: +188/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: PHP - Date What format
« Reply #7 on: 29 Jul 2020, 12:52:18 pm »
BOTH are valid approaches, when doing simple time conversions there's still nothing wrong with Date. In fact I would argue AGAINST using the wasteful "procedural wrappers" for DateTime just as I would for mysqli.

If I were to do that now, I'd do:

Code: [Select]
(new DateTime($r['expiry']))->format('F dS Y')
Depending on what you're doing I might store that DateTime in a variable. DateTime is nice as it has a slew of methods you can use, AND a bunch of RFC date formats predeclared inside it.

https://www.php.net/manual/en/class.datetime.php

So for example if you need the format a cookie uses and the RFC that the <time> tag expects on its datetime attribute? You can:

Code: [Select]
$date = new DateTime($row['expiry']);

$cookieDate = $date->format(DateTime::COOKIE);
$dateTime = $date->format(DateTime::RFC3339);

The fun part is those DateTimeInterface static properties? You can use them with the old date functions too.
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