Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    52
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Php Time() Questions

    Hello.
    I want to use Time() to store dates in my database instead of Date() because with Time I can check latest records of the week, day, etc..
    So I have few questions:

    1) What is better to use, Time Or Date function? (big cms)

    2) Is there any function that converts the unix time provided in the time() function into date()?

    2) Is there any function that converts the unix time provided in the time() function into "This day","This week","This hour",etc..?

    Thanks in advance.
    Need domain? Buy Cheap Domain at Rapid Host :)

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    1 - depands on what you want.
    2 - date() is function ur looking for date('format', $timestamp)
    3 - unix timestamp is number of seconds since speciffic point in time so just decrese that value by say number of seconds in one day/week/hour/whatever

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    1. DateTime datatype
    2. Date() as mentioned
    3. No, that's manual, unlike going from "this friday" to create time, you cannot go from integer (or datetime) to "this friday"


    Don't use an integer datatype from php's time() function to store records. Use datetime datatype from SQL to do so. It is much easier to use conditional checks on datetime datatypes in SQL than it is to do so with an integer. "this", "current", and any relative type can only be manually calculated, but would be relatively simple if you can use DateInterval and the DateTime::diff.

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    52
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    1. DateTime datatype
    2. Date() as mentioned
    3. No, that's manual, unlike going from "this friday" to create time, you cannot go from integer (or datetime) to "this friday"


    Don't use an integer datatype from php's time() function to store records. Use datetime datatype from SQL to do so. It is much easier to use conditional checks on datetime datatypes in SQL than it is to do so with an integer. "this", "current", and any relative type can only be manually calculated, but would be relatively simple if you can use DateInterval and the DateTime::diff.
    When Im using datatime the record changes to something like this:
    12-11-13 21:54:53

    And its harder to check like that "Last week hits" and data like that..
    Need domain? Buy Cheap Domain at Rapid Host :)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not at all. Last week can be declared by using datatypes of "last x" for your datetime syntax:
    PHP Code:
    $dtLastSunday = new DateTime('Sunday -2 week');
    $dtLastSaturday = new DateTime('Saturday -1 week');
    printf("Last sunday was %s, and last saturday was %s" PHP_EOL,
        
    $dtLastSunday->format('Y-m-d'), $dtLastSaturday->format('Y-m-d')); 
    For example. Those formats will work in a WHERE clause for a date between. That above is the sunday to saturday of last week (not the last sunday which is deceiving in the text I used).

    SQL has its own utilities to do this using date_sub and modulus. I don't think there exists a relative concept to it, but you can pull weekdays and things like that. You may want to check with the SQL guys for that one since I can't test that where I am.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •