...

View Full Version : Php Time() Questions



topdown.me
11-08-2012, 06:25 PM
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.

patryk
11-08-2012, 07:02 PM
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

Fou-Lu
11-08-2012, 07:08 PM
DateTime datatype
Date() as mentioned
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.

topdown.me
11-08-2012, 07:55 PM
DateTime datatype
Date() as mentioned
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..

Fou-Lu
11-08-2012, 09:07 PM
Not at all. Last week can be declared by using datatypes of "last x" for your datetime syntax:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum