...

View Full Version : Resolved Can someone convert my time and date to seconds?



auriaks
03-03-2010, 07:53 PM
Hi,

I have time and date variables:



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


Can someone give me a php script which would count and sum their both seconds.

I would be really thankful for that :thumbsup:

tomws
03-03-2010, 08:05 PM
http://php.net/time

Description
int time ( void )

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

auriaks
03-03-2010, 08:16 PM
http://php.net/time

Some misunderstanding... I need seconds of time and date. Like today in seconds would be like 1021524521142 seconds :D

Fou-Lu
03-03-2010, 08:19 PM
Some misunderstanding... I need seconds of time and date. Like today in seconds would be like 1021524521142 seconds :D

Thats precisely what time() does and what date() will use when not provided a second argument. Time() is what will fit into you're current code.

auriaks
03-03-2010, 08:35 PM
when I write echo "$time and $date"; it shows 20:38:12 and 2010-03-03

What I have to write to get answer 1884546131548?

It seems that i haven't understood something yet

Fou-Lu
03-03-2010, 08:47 PM
You'll never get that number anyway, its too big for PHP to store in an integer.
The largest number PHP can store is 2147483647, nearly twice that for display with unsigned %u currently.

Perhaps you're thinking that time starts at year 0, but time starts at epoch December 31 1969 / January 1 1970.

auriaks
03-03-2010, 09:01 PM
hmm, the thing is, that I want to compare saved time and date yesterday 2010-03-02 21:22:18 and today's time and date 2010-03-03 23:00:00


if ((today time and date) <= (yesterday time and date + 6 minutes)) {
echo "the time is same";
} else {
echo "Go away! Your time is too old";
}

how I can do this then?

tomws
03-03-2010, 09:04 PM
If you'd take the time to consult the documentation as linked in post #2, you'd be well on your way to solving the problem.

auriaks
03-03-2010, 10:19 PM
I dont know how to use this with time:


date('Y-m-d', strtotime('+6 minutes'))

Fou-Lu
03-04-2010, 02:36 PM
Thats correct, you'll never see a difference though compared to no second parameter since 6 minutes will only effect the last 6 minutes of the day and you have not shown the time above.
Use this to also display the time:


print date('Y-m-d H:i:s', strtotime('+6 minutes'));



BTW, just noticed you had another post above. Comparisons of dates are always a pain. This is why using a pure PHP approach, I'd recommend always using integers as you're datatype for storing times. The date() function is useful for formatting the date from an integer timestamp, but strtotime itself is limited in what its allowed to parse. If you're using MySQL or another database engine, most modern ones can process dates using things like datediff and datesub/add. These let you do some comparisons between you're dates and not concern yourself with the limitations of the 32-bit integer (don't worry we still have 20 some odd years).

auriaks
03-05-2010, 01:50 AM
it is possible to do something like that?


$time = ((date('Y-m-d H:i:s', strtotime('-6 minutes')))-(date('Y-m-d H:i:s', strtotime('+6 minutes'))));

Fou-Lu
03-05-2010, 02:31 PM
Not unless you want to compare strings.
You can however subtract the results of strtotime and then process it as a date.

$time = date('Y-m-d H:i:s', (strtotime('-6 minutes') - strtotime('+6 minutes')));


strtotime itself can take a second parameter to indicate the offset result we're applying to. By default this is time(), but it can be a prestored timestamp if you want.

auriaks
03-05-2010, 03:02 PM
Not unless you want to compare strings.


Yes, thats what I want... I need to compare two times. All I have done now, I just compared two times like this:


$time1
$time2
if ($time1 <> $time2) {you are off} else {you are on}

but what if I need exactly the difference of these two times? in minutes or seconds...

tomws
03-05-2010, 03:07 PM
Yes, thats what I want

No, that's not what you want. You want to compare times. While strings can be used (compared) in very specific circumstances, you'd be better served by converting to timestamps (integers) and comparing those. Then it's just simple math.

auriaks
03-05-2010, 03:21 PM
how is timestamp() different from date() ?

tomws
03-05-2010, 03:26 PM
See my signature. See also Fou's quite helpful posts earlier in the thread.

Fou-Lu
03-05-2010, 03:38 PM
Hah, this is a problem with PHP.
PHP is called a datatype weak language, so most of the variables are handled behind the scenes independant of the writer.
The result of time() is an integer value, a number that can be easily compared to with another number. This is also called a timestamp.
The result of date() is a string, a formatted representation of the time. Date is useful for displaying the results of time. Strings are difficult to compare with precision control, though a simple strcmp will tell you if one is considered 'bigger' than the other, but you would see shortly after that what a string considers 'bigger' is different than what we consider 'bigger' (for dates, we would consider future dates to be bigger than current dates; strings will not).

With newer versions of php (5.2+), we've seen added functionality to date/time handling with the DateTime (http://php.ca/manual/en/class.datetime.php) class. These are actually designed to simplify our process of adding and subtracting specific values with a nested DateInterval class, and more easily comparable date/time values. Object Oriented approaches with PHP is a more intermediate skill (unless you're familiar with OOP from another language).

auriaks
03-05-2010, 04:07 PM
I get it... I must save my time with timestamp in mysql database. What I have to write in spite of this when I am detecting current time with timestamp? because now I use


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

I will get $time as a number from database, but I have to compare with value of current time...

Fou-Lu
03-05-2010, 04:57 PM
I get it... I must save my time with timestamp in mysql database. What I have to write in spite of this when I am detecting current time with timestamp? because now I use


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

I will get $time as a number from database, but I have to compare with value of current time...

If timestamp is used in MySQL storage, you can format it with:


$datetime = date('Y-m-d H:i:s', $timeFromSQL);

Or separate date / time into individual variables.

auriaks
03-05-2010, 05:45 PM
As I was thinking, there was much more easier way to do this... I finished it perfectly without timestamps.

I can't believe that none had told me before that there is explode() function available. :(

tomws
03-05-2010, 05:56 PM
I can't believe that none had told me before that there is explode() function available. :(

Because explode is best used for text processing and you are dealing with times which are far easier to handle with other means, but since you continue to fail to grasp that, here are links to some other unrelated functions that also won't make the task easier:

http://php.net/curl_exec
http://php.net/parse_ini_file
http://php.net/is_float

Now you can't complain because no one ever told you that those functions are available. :rolleyes:

auriaks
03-05-2010, 07:38 PM
No, I can't understand why explode haven't got something in common with time counting to seconds... I used it and I really think tjat they have something in common :D Btw, thanks for the curl exec link... :)

In common, I haven't blamed no one, I am just saying, that I worked with Date string where we must use something with text functions.. Correct me if I'm wrong.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum