Date conversions ( date, mktime )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mickey

    Date conversions ( date, mktime )

    Hi,

    I have a date in three pieces dd mm yyyy.
    I'm just wondering the best way to convert this for inserting into a
    DATETIME field and then back again when displaying it.

    I have tried date, strtotime mktime but it keeps inserting a blank
    date. i.e. 00:00:00 00:00...

    I've been looking through the php manual but I can't seem to get this
    working.
    Also would I be better off using the UNIX_TIMESTAMP function?

    Thanks in advance.

  • Chris Hope

    #2
    Re: Date conversions ( date, mktime )

    Mickey wrote:
    I have a date in three pieces dd mm yyyy.
    I'm just wondering the best way to convert this for inserting into a
    DATETIME field and then back again when displaying it.
    >
    I have tried date, strtotime mktime but it keeps inserting a blank
    date. i.e. 00:00:00 00:00...
    >
    I've been looking through the php manual but I can't seem to get this
    working.
    Also would I be better off using the UNIX_TIMESTAMP function?
    You don't need to convert it. If you have three variables, for example
    $year, $month and $day, all you need to do is this:

    "INSERT INTO sometable (..., mydatefield) VALUES
    (..., '$year-$month-$day')"

    There are better ways of doing this but at its most simple that will
    work. Of course you need to be aware of sql injection if your values
    come from a form post, and the $month and $day variables need to be
    zero padded.

    To then utilise the database value from your SELECT query you can use
    strtotime() to convert it to a timestamp and date() to format you as
    you require, but there are limitations on the date range. You may be
    better to use the database's date formatting functions to have it
    formatted as you would like it displayed.

    --
    Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

    Comment

    • Mickey

      #3
      Re: Date conversions ( date, mktime )

      On Mar 12, 6:54 pm, Chris Hope <blackh...@elec trictoolbox.com wrote:
      You don't need to convert it. If you have three variables, for example
      $year, $month and $day, all you need to do is this:
      >
      "INSERT INTO sometable (..., mydatefield) VALUES
      (..., '$year-$month-$day')"
      >
      There are better ways of doing this but at its most simple that will
      work. Of course you need to be aware of sql injection if your values
      come from a form post, and the $month and $day variables need to be
      zero padded.
      >
      To then utilise the database value from your SELECT query you can use
      strtotime() to convert it to a timestamp and date() to format you as
      you require, but there are limitations on the date range. You may be
      better to use the database's date formatting functions to have it
      formatted as you would like it displayed.
      >
      --
      Chris Hope |www.electricto olbox.com|www.linuxcdmall.com
      Thanks for the reply Chris.
      I also just found this which may be of help to someone else.

      Thanks again.

      Comment

      • Mickey

        #4
        Re: Date conversions ( date, mktime )

        On Mar 12, 7:32 pm, "Mickey" <mickey.allr... @gmail.comwrote :
        On Mar 12, 6:54 pm, Chris Hope <blackh...@elec trictoolbox.com wrote:
        >
        >
        >
        You don't need to convert it. If you have three variables, for example
        $year, $month and $day, all you need to do is this:
        >
        "INSERT INTO sometable (..., mydatefield) VALUES
        (..., '$year-$month-$day')"
        >
        There are better ways of doing this but at its most simple that will
        work. Of course you need to be aware of sql injection if your values
        come from a form post, and the $month and $day variables need to be
        zero padded.
        >
        To then utilise the database value from your SELECT query you can use
        strtotime() to convert it to a timestamp and date() to format you as
        you require, but there are limitations on the date range. You may be
        better to use the database's date formatting functions to have it
        formatted as you would like it displayed.
        >
        --
        Chris Hope |www.electricto olbox.com|www.linuxcdmall.com
        >
        Thanks for the reply Chris.
        I also just found this which may be of help to someone else.
        >
        Thanks again.
        ^
        Forgot the link.
        This: http://lists.evolt.org/archive/Week-...02/162500.html

        Comment

        Working...