Skip to main content

Forums

This topic contains 4 replies, has 3 voices, and was last updated by  __ 4 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #205457

    bearhead
    Participant

    I’m working on a php script that gets some records from a database, and then orders them based on their date.

    The issue I’m having is that the dates were entered like 7-24-15, instead of 07-24-15, so October, November, and December dates are all coming before February.

    Any ideas on how I could get the dates to order like they should?

    #205459

    Alen
    Participant

    This reply has been reported for inappropriate content.

    @bearhead

    Have you ever used Carbon PHP library.

    Maybe you can explode('-', $data); the date coming from the database and use Carbon’s Carbon::createFromDate(2015, 7, 24); that will allow you to do bunch of other things. See Carbon documentation for more information.

    #205476

    __
    Participant

    This reply has been reported for inappropriate content.

    I’m working on a php script that gets some records from a database, and then orders them based on their date.

    I highly recommend you do not do this. Sort the records in the database.

    The issue I’m having is that the dates were entered like 7-24-15, instead of 07-24-15 …

    Ideally, you would fix the stored records in the database. Actually, you should be using DATETIME or TIMESTAMP columns, not VARCHAR or some other kind of text.

    I highly recommend updating your schema if at all possible.

    use Carbon’s Carbon::createFromDate

    You might like using a library like Carbon, but also be aware that php has a native DateTime class which will do what you need, and be much faster. For example,

    <?php
    $badTime  = "7-24-15";
    $goodTime = DateTime::createFromFormat( "n-d-Y",$badTime )
        ->format( "m-d-Y" );
    

    If the days also have no leading 0’s, DateTime::createFromFormat() should use “n-j-Y” instead of “n-d-Y”.

    #205644

    bearhead
    Participant

    This reply has been reported for inappropriate content.

    Thanks for the suggestions. Yeah, datetime or timestamp columns would make alot more sense, however, the dates are coming in automatically from another source like “7/27/2015 12:00:00 AM”, so if I try to use something other than varchar, the dates get zeroed out.

    I supposed I could add an additional column that is not automatically populated, and then just type the date for each record in manually, but that kind of defeats the purpose of the automated system I was after…

    If I wanted to go the php datetime route, would that mean that I would have to get all the records into an array and then sort the array? I’m hoping to be able to sort them using SORT BY in my database query.

    #205690

    __
    Participant

    This reply has been reported for inappropriate content.

    If I wanted to go the php datetime route, would that mean that I would have to get all the records into an array and then sort the array?

    Yes, which is why it (and any php solution) is the least preferable approach. It is not efficient.

    datetime or timestamp columns would make alot more sense, however, the dates are coming in automatically from another source…

    I would be looking for a way to format them properly in the DB — on insert, preferably. Updating values already in your DB shouldn’t be difficult.

    If you want help with this, describe how he database is populated and we’ll see what we can figure out.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star