How can I tell if a time was yesterday?

How can I tell if a time was yesterday?

am 15.08.2007 11:57:38 von FFMG

Hi,

Given a time I want to know if it is today or yesterday, (on the
server).

I cannot use time difference because if I log the time and it is 23:59
and I check again at 24:01 then the earlier time is, in fact,
yesterday.

so given time $a and time $b how can I calculate if $a is the day
before $b?

Thanks

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------ ------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=19402

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:15:55 von alvaro.NOSPAMTHANKS

FFMG escribió:
> Given a time I want to know if it is today or yesterday, (on the
> server).

function is_yesterday($timestamp){ // NOT TESTED
$start = mktime(0, 0, 0,
date('n', time()), date('j', time()), date('Y', time()));
$end = mktime(0, 0, 0,
date('n', time()), date('j', time()), date('Y', time()));

return ($timestamp>=$start) && ($timestamp<$end)
}


--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:18:19 von luiheidsgoeroe

On Wed, 15 Aug 2007 11:57:38 +0200, FFMG m> =

wrote:
> Given a time I want to know if it is today or yesterday, (on the
> server).
>
> I cannot use time difference because if I log the time and it is 23:59=

> and I check again at 24:01 then the earlier time is, in fact,
> yesterday.
>
> so given time $a and time $b how can I calculate if $a is the day
> before $b?


What format is your time in? 24:01 is highly unusual to use. If you got =
=

real timestamps, you can create & compare any timestamp using one of the=
=

date formatting functions.

function is_today($timestamp){
return date('d',$timestamp) == date('d');
}
-- =

Rik Wasmus

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:23:52 von gosha bine

On 15.08.2007 11:57 FFMG wrote:
> Hi,
>
> Given a time I want to know if it is today or yesterday, (on the
> server).
>
> I cannot use time difference because if I log the time and it is 23:59
> and I check again at 24:01 then the earlier time is, in fact,
> yesterday.
>
> so given time $a and time $b how can I calculate if $a is the day
> before $b?
>

if(unixtojd($b) - unixtojd($a) == 1)

http://www.php.net/manual/en/function.unixtojd.php

if the dates are in Mysql you'd be better off with TO_DAYS()


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:26:32 von luiheidsgoeroe

On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine =

wrote:

> On 15.08.2007 11:57 FFMG wrote:
>> Hi, Given a time I want to know if it is today or yesterday, (on the=

>> server).
>> I cannot use time difference because if I log the time and it is 23:=
59
>> and I check again at 24:01 then the earlier time is, in fact,
>> yesterday.
>> so given time $a and time $b how can I calculate if $a is the day
>> before $b?
>>
>
> if(unixtojd($b) - unixtojd($a) == 1)
>
> http://www.php.net/manual/en/function.unixtojd.php


Erm, Julian days start at noon?
-- =

Rik Wasmus

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:41:05 von gosha bine

On 15.08.2007 12:26 Rik wrote:
> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine
> wrote:
>
>> On 15.08.2007 11:57 FFMG wrote:
>>> Hi, Given a time I want to know if it is today or yesterday, (on the
>>> server).
>>> I cannot use time difference because if I log the time and it is 23:59
>>> and I check again at 24:01 then the earlier time is, in fact,
>>> yesterday.
>>> so given time $a and time $b how can I calculate if $a is the day
>>> before $b?
>>>
>>
>> if(unixtojd($b) - unixtojd($a) == 1)
>>
>> http://www.php.net/manual/en/function.unixtojd.php
>
>
> Erm, Julian days start at noon?

Don't trust every comment you see in the manual. ;)



--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: How can I tell if a time was yesterday?

am 15.08.2007 12:54:36 von luiheidsgoeroe

On Wed, 15 Aug 2007 12:41:05 +0200, gosha bine =

wrote:

> On 15.08.2007 12:26 Rik wrote:
>> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine =
=

>> wrote:
>>
>>> On 15.08.2007 11:57 FFMG wrote:
>>>> Hi, Given a time I want to know if it is today or yesterday, (on t=
he
>>>> server).
>>>> I cannot use time difference because if I log the time and it is =

>>>> 23:59
>>>> and I check again at 24:01 then the earlier time is, in fact,
>>>> yesterday.
>>>> so given time $a and time $b how can I calculate if $a is the day
>>>> before $b?
>>>>
>>>
>>> if(unixtojd($b) - unixtojd($a) == 1)
>>>
>>> http://www.php.net/manual/en/function.unixtojd.php
>> Erm, Julian days start at noon?
>
> Don't trust every comment you see in the manual. ;)

Haven't examined the function, but I did see that comment yeah, and =

wikipedia (well, another source of blatant lies sometimes, but hey) stat=
es:


[quote]
The Julian day or Julian day number (JDN) is the integer number of days =
=

that have elapsed since the initial epoch defined as noon Universal Time=
=

(UT) Monday, January 1, 4713 BC in the proleptic Julian calendar [1]. Th=
at =

noon-to-noon day is counted as Julian day 0. Thus the multiples of 7 are=
=

Mondays. Negative values can also be used, although those predate all =

recorded history.

Now at 00:20, Friday August 10, 2007 (UTC) the JDN is 2454322. The =

remainder of this value divided by 7 is 3, an integer expression for the=
=

day of the week with 0 representing Monday.

The Julian date (JD) is a continuous count of days and fractions elapsed=
=

since the same initial epoch. Currently the JD is 2454322.51389. The =

integral part (its floor) gives the Julian day number. The fractional pa=
rt =

gives the time of day since noon UT as a decimal fraction of one day or =
=

fractional day, with 0.5 representing midnight UT. Typically, a 64-bit =

floating point (double precision) variable can represent an epoch =

expressed as a Julian date to about 1 millisecond precision.
[/quote]

Which led me to assume it to be true.

Then again, the manual points at =

, which states there are severa=
l =

posibilities to start the day... If you say the PHP function takes =

midnight, I believe you :-)
-- =

Rik Wasmus

Re: How can I tell if a time was yesterday?

am 15.08.2007 13:24:58 von FFMG

Rik;87140 Wrote:
> On Wed, 15 Aug 2007 11:57:38 +0200, FFMG
>
> wrote:
> > Given a time I want to know if it is today or yesterday, (on the
> > server).
> >
> > I cannot use time difference because if I log the time and it is
> 23:59
> > and I check again at 24:01 then the earlier time is, in fact,
> > yesterday.
> >
> > so given time $a and time $b how can I calculate if $a is the day
> > before $b?
>
>
> What format is your time in? 24:01 is highly unusual to use. If you got
>
> real timestamps, you can create & compare any timestamp using one of
> the
> date formatting functions.
>
> function is_today($timestamp){
> return date('d',$timestamp) == date('d');
> }
> --
> Rik Wasmus

Sorry you are right, I was trying to illustrate my point.

I am using unix timestamp.

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------ ------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=19402

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

Re: How can I tell if a time was yesterday?

am 15.08.2007 13:28:07 von gosha bine

On 15.08.2007 12:54 Rik wrote:
> On Wed, 15 Aug 2007 12:41:05 +0200, gosha bine
> wrote:
>
>> On 15.08.2007 12:26 Rik wrote:
>>> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine
>>> wrote:
>>>
>>>> On 15.08.2007 11:57 FFMG wrote:
>>>>> Hi, Given a time I want to know if it is today or yesterday, (on the
>>>>> server).
>>>>> I cannot use time difference because if I log the time and it is
>>>>> 23:59
>>>>> and I check again at 24:01 then the earlier time is, in fact,
>>>>> yesterday.
>>>>> so given time $a and time $b how can I calculate if $a is the day
>>>>> before $b?
>>>>>
>>>>
>>>> if(unixtojd($b) - unixtojd($a) == 1)
>>>>
>>>> http://www.php.net/manual/en/function.unixtojd.php
>>> Erm, Julian days start at noon?
>>
>> Don't trust every comment you see in the manual. ;)
>
> Haven't examined the function, but I did see that comment yeah, and
> wikipedia (well, another source of blatant lies sometimes, but hey) states:
>
>
> [quote] skipped
>
> Which led me to assume it to be true.
>
> Then again, the manual points at
> , which states there are
> several posibilities to start the day... If you say the PHP function
> takes midnight, I believe you :-)

php implementation is not that clever. It just pulls the date (year,
month, day) out of the timestamp[*] and calculates jd for this date.
It's completely unaware of times.

[*] Of course, the result of conversion depends on the local time
settings i.e. timezone and daylight saving state.



--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: How can I tell if a time was yesterday?

am 15.08.2007 20:45:33 von Toby A Inkster

Rik wrote:

> function is_today($timestamp){
> return date('d',$timestamp) == date('d');
> }

The following will return TRUE:

is_today(strtotime('15 Feb 1986'));

Better would be:

function is_today ($ts)
{ return date('Ymd',$ts)==date('Ymd'); }

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 55 days, 22:22.]

Fake Steve is Dead; Long Live Fake Bob!
http://tobyinkster.co.uk/blog/2007/08/13/fake-bob/

Re: How can I tell if a time was yesterday?

am 15.08.2007 21:08:05 von luiheidsgoeroe

On Wed, 15 Aug 2007 20:45:33 +0200, Toby A Inkster =

wrote:

> Rik wrote:
>
>> function is_today($timestamp){
>> return date('d',$timestamp) == date('d');
>> }
>
> The following will return TRUE:
>
> is_today(strtotime('15 Feb 1986'));
>
> Better would be:
>
> function is_today ($ts)
> { return date('Ymd',$ts)==date('Ymd'); }

Up it does. Kept it as simple as possible as the op stated it could only=
=

be today or yesterday: hence not a month difference etc.

Then again this will be up for it when the difference _can_ run longer =

indeed.

-- =

Rik Wasmus

Re: How can I tell if a time was yesterday?

am 15.08.2007 23:48:17 von alvaro.NOSPAMTHANKS

Álvaro G. Vicario escribió:
> FFMG escribió:
>> Given a time I want to know if it is today or yesterday, (on the
>> server).
>
> function is_yesterday($timestamp){ // NOT TESTED
> $start = mktime(0, 0, 0,
> date('n', time()), date('j', time()), date('Y', time()));
> $end = mktime(0, 0, 0,
> date('n', time()), date('j', time()), date('Y', time()));
>
> return ($timestamp>=$start) && ($timestamp<$end)
> }

I'm sorry, I didn't understand the specs and my code contains a huge
type that makes it useless; $end should be;

$end = mktime(0, 0, 0, date('n', time()), date('j', time())+1, date('Y',
time()));

Anyway, I hope it can at least give some ideas...

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--