php to get date and time separately into MySQL?

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

    php to get date and time separately into MySQL?

    Hello,

    at the moment I can add the combined date and time into MySQL using
    php

    $dt1 = date("Y-m-d H:i:s");

    is it possible to add the date and time separately?

    I thought it might be

    $dt1 = date("Y-m-d");

    and

    $dt2 = time("H:i:s");

    but this doesn't seem to work ...

    Cheers

    Geoff
  • Rik Wasmus

    #2
    Re: php to get date and time separately into MySQL?

    On Sun, 18 May 2008 01:17:11 +0200, Geoff Cox <gcox@freeuk.no tcomwrote:
    Hello,
    >
    at the moment I can add the combined date and time into MySQL using
    php
    >
    $dt1 = date("Y-m-d H:i:s");
    >
    is it possible to add the date and time separately?
    >
    I thought it might be
    >
    $dt1 = date("Y-m-d");
    >
    and
    >
    $dt2 = time("H:i:s");
    $dt2 = date("H:i:s");

    Why would the funtion name change?
    --
    Rik Wasmus
    ....spamrun finished

    Comment

    • Michael Austin

      #3
      Re: php to get date and time separately into MySQL?

      Geoff Cox wrote:
      Hello,
      >
      at the moment I can add the combined date and time into MySQL using
      php
      >
      $dt1 = date("Y-m-d H:i:s");
      >
      is it possible to add the date and time separately?
      >
      I thought it might be
      >
      $dt1 = date("Y-m-d");
      >
      and
      >
      $dt2 = time("H:i:s");
      >
      but this doesn't seem to work ...
      >
      Cheers
      >
      Geoff
      I would agree that while your question IS a PHP question, the solution
      may be a MySQL solution. Next time you may consider a cross-post (both
      newsgroups in the TO: field :) )

      You state "doesn't seem to work"... What is the error and/or result and
      what is the result you are expecting. What is the data-type of the
      column you are attempting to store this information? CHAR? VARCHAR?
      DATE? TIMESTAMP? Just remember, while you can store a date in a
      CHAR/VARCHAR column, doing date arithmetic on it will still require
      converting it to date/time.

      What is the end goal and why? The "why" (short explanation will do)
      would determine which one of a thousand methods you could or should be
      using to make things easy for you, your app and your db. If your
      database has a DATETIME and TIMESTAMP column you could use the MySQL
      function NOW() or the PHP time() both of which derive the "unix
      timestamp" which is number of seconds from EPOCH time for that platform.
      Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
      01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
      00:00.

      See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
      explanations of date/time data-types.

      Any formating or retrieving time ranges can be derived from this
      DATETIME or TIMESTAMP field. I tend to let the database do the database
      time and the parse that date/time. **Generally** speaking, time without
      a date is pretty useless YMMV. Dates without time *can* be useful for
      things like past/future shipping dates etc or any one of many, many more
      examples that could be conceived.

      IMO saying that something did or will occur at 2PM is pretty useless
      without knowing which date. And if this information is required for
      reporting that "most queries are performed between 2 and 3AM", it can be
      easily derived from the full timestamp which would also require one less
      column to maintain.

      Comment

      • Jerry Stuckle

        #4
        Re: php to get date and time separately into MySQL?

        Michael Austin wrote:
        Geoff Cox wrote:
        >Hello,
        >>
        >at the moment I can add the combined date and time into MySQL using
        >php
        >>
        >$dt1 = date("Y-m-d H:i:s");
        >>
        >is it possible to add the date and time separately?
        >>
        >I thought it might be
        >>
        >$dt1 = date("Y-m-d");
        >>
        >and
        >>
        >$dt2 = time("H:i:s");
        >>
        >but this doesn't seem to work ...
        >>
        >Cheers
        >>
        >Geoff
        >
        I would agree that while your question IS a PHP question, the solution
        may be a MySQL solution. Next time you may consider a cross-post (both
        newsgroups in the TO: field :) )
        >
        Nope, this is a pure PHP question.
        You state "doesn't seem to work"... What is the error and/or result and
        what is the result you are expecting. What is the data-type of the
        column you are attempting to store this information? CHAR? VARCHAR?
        DATE? TIMESTAMP? Just remember, while you can store a date in a
        CHAR/VARCHAR column, doing date arithmetic on it will still require
        converting it to date/time.
        >
        What is the end goal and why? The "why" (short explanation will do)
        would determine which one of a thousand methods you could or should be
        using to make things easy for you, your app and your db. If your
        database has a DATETIME and TIMESTAMP column you could use the MySQL
        function NOW() or the PHP time() both of which derive the "unix
        timestamp" which is number of seconds from EPOCH time for that platform.
        Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
        01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
        00:00.
        >
        See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
        explanations of date/time data-types.
        >
        Any formating or retrieving time ranges can be derived from this
        DATETIME or TIMESTAMP field. I tend to let the database do the database
        time and the parse that date/time. **Generally** speaking, time without
        a date is pretty useless YMMV. Dates without time *can* be useful for
        things like past/future shipping dates etc or any one of many, many more
        examples that could be conceived.
        >
        IMO saying that something did or will occur at 2PM is pretty useless
        without knowing which date. And if this information is required for
        reporting that "most queries are performed between 2 and 3AM", it can be
        easily derived from the full timestamp which would also require one less
        column to maintain.
        >
        The problem is he should be using

        $dt2 = date("H:i:s");

        There are many reasons why you might want date and time data in
        different columns. But that is not part of this problem.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Geoff Cox

          #5
          Re: php to get date and time separately into MySQL?

          On Sun, 18 May 2008 01:33:39 +0200, "Rik Wasmus"
          <luiheidsgoeroe @hotmail.comwro te:
          >On Sun, 18 May 2008 01:17:11 +0200, Geoff Cox <gcox@freeuk.no tcomwrote:
          >
          >Hello,
          >>
          >at the moment I can add the combined date and time into MySQL using
          >php
          >>
          >$dt1 = date("Y-m-d H:i:s");
          >>
          >is it possible to add the date and time separately?
          >>
          >I thought it might be
          >>
          >$dt1 = date("Y-m-d");
          >>
          >and
          >>
          >$dt2 = time("H:i:s");
          >
          >$dt2 = date("H:i:s");
          >
          >Why would the funtion name change?
          OK - my mistake - thanks!

          Cheers

          Geoff

          Comment

          • Geoff Cox

            #6
            Re: php to get date and time separately into MySQL?

            On Sat, 17 May 2008 21:54:05 -0500, Michael Austin
            <maustin@firstd basource.comwro te:
            >Geoff Cox wrote:
            >Hello,
            >>
            >at the moment I can add the combined date and time into MySQL using
            >php
            >>
            >$dt1 = date("Y-m-d H:i:s");
            >>
            >is it possible to add the date and time separately?
            >>
            >I thought it might be
            >>
            >$dt1 = date("Y-m-d");
            >>
            >and
            >>
            >$dt2 = time("H:i:s");
            >>
            >but this doesn't seem to work ...
            >>
            >Cheers
            >>
            >Geoff
            >
            >I would agree that while your question IS a PHP question, the solution
            >may be a MySQL solution. Next time you may consider a cross-post (both
            >newsgroups in the TO: field :) )
            >
            >You state "doesn't seem to work"... What is the error and/or result and
            >what is the result you are expecting. What is the data-type of the
            >column you are attempting to store this information? CHAR? VARCHAR?
            >DATE? TIMESTAMP? Just remember, while you can store a date in a
            >CHAR/VARCHAR column, doing date arithmetic on it will still require
            >converting it to date/time.
            >
            >What is the end goal and why? The "why" (short explanation will do)
            >would determine which one of a thousand methods you could or should be
            >using to make things easy for you, your app and your db. If your
            >database has a DATETIME and TIMESTAMP column you could use the MySQL
            >function NOW() or the PHP time() both of which derive the "unix
            >timestamp" which is number of seconds from EPOCH time for that platform.
            Epoch time for most UNIX OS's is 01/01/1970, for Windows it is
            >01/01/1980 00:00 (and for OpenVMS (formerly DEC VMS) it is 17-Nov-1858
            >00:00.
            >
            >See http://dev.mysql.com/doc/refman/5.0/en/datetime.html for further
            >explanations of date/time data-types.
            >
            >Any formating or retrieving time ranges can be derived from this
            >DATETIME or TIMESTAMP field. I tend to let the database do the database
            >time and the parse that date/time. **Generally** speaking, time without
            >a date is pretty useless YMMV. Dates without time *can* be useful for
            >things like past/future shipping dates etc or any one of many, many more
            >examples that could be conceived.
            >
            >IMO saying that something did or will occur at 2PM is pretty useless
            >without knowing which date. And if this information is required for
            >reporting that "most queries are performed between 2 and 3AM", it can be
            >easily derived from the full timestamp which would also require one less
            >column to maintain.

            Many thanks for your detailed reply. I will read with case.

            I think I have gone back to having date and time together - my problem
            was with getting the date/time value into Access but have found a way
            round that.

            Cheers

            Geoff

            Comment

            • Guillaume

              #7
              Re: php to get date and time separately into MySQL?

              Rik Wasmus a écrit :
              >$dt1 = date("Y-m-d");
              >>
              >and
              >>
              >$dt2 = time("H:i:s");
              Btw I'd rather do
              $time = time();
              $dt1 = date("Y-m-d", $time);
              $dt2 = date("H:i:s", $time);

              That way, if there is any problem on the server for example, you work
              with the same exact time.

              Let's say you have a HUGE backup script working at midnight today, using
              all of your CPU.

              At 23:59:59 today, your page is called, and the first line is (slowly)
              parsed.
              $dt1 = '2008-05-19';

              Then, the second line, due to the CPU being highly used, comes a second
              later.
              $dt2 = '00:00:00';

              And there you go with an incorrect time.

              Regards,
              --
              Guillaume

              Comment

              Working...