How can I tell if a time was yesterday?

This is a discussion on How can I tell if a time was yesterday? within the PHP Language forums, part of the PHP Programming Forums category; Hi, Given a time I want to know if it is today or yesterday, (on the server). I cannot use ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-15-2007
FFMG
 
Posts: n/a
Default How can I tell if a time was yesterday?


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

Reply With Quote
  #2 (permalink)  
Old 08-15-2007
Álvaro G. Vicario
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

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
--
Reply With Quote
  #3 (permalink)  
Old 08-15-2007
Rik
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

On Wed, 15 Aug 2007 11:57:38 +0200, FFMG <FFMG.2vcq3z@no-mx.httppoint.com>
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
Reply With Quote
  #4 (permalink)  
Old 08-15-2007
gosha bine
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

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
Reply With Quote
  #5 (permalink)  
Old 08-15-2007
Rik
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine <stereofrog@gmail.com>
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
Reply With Quote
  #6 (permalink)  
Old 08-15-2007
gosha bine
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

On 15.08.2007 12:26 Rik wrote:
> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine <stereofrog@gmail.com>
> 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
Reply With Quote
  #7 (permalink)  
Old 08-15-2007
Rik
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

On Wed, 15 Aug 2007 12:41:05 +0200, gosha bine <stereofrog@gmail.com>
wrote:

> On 15.08.2007 12:26 Rik wrote:
>> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine <stereofrog@gmail.com>
>> 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:

<http://en.wikipedia.org/wiki/Julian_day>
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]. That
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 part
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.
Which led me to assume it to be true.

Then again, the manual points at
<http://www.hermetic.ch/cal_stud/jdn.htm>, which states there are several
posibilities to start the day... If you say the PHP function takes
midnight, I believe you :-)
--
Rik Wasmus
Reply With Quote
  #8 (permalink)  
Old 08-15-2007
FFMG
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?


Rik;87140 Wrote:
> On Wed, 15 Aug 2007 11:57:38 +0200, FFMG
> <FFMG.2vcq3z@no-mx.httppoint.com>
> 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).

Reply With Quote
  #9 (permalink)  
Old 08-15-2007
gosha bine
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

On 15.08.2007 12:54 Rik wrote:
> On Wed, 15 Aug 2007 12:41:05 +0200, gosha bine <stereofrog@gmail.com>
> wrote:
>
>> On 15.08.2007 12:26 Rik wrote:
>>> On Wed, 15 Aug 2007 12:23:52 +0200, gosha bine <stereofrog@gmail.com>
>>> 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:
>
> <http://en.wikipedia.org/wiki/Julian_day>
> [quote] skipped
>
> Which led me to assume it to be true.
>
> Then again, the manual points at
> <http://www.hermetic.ch/cal_stud/jdn.htm>, 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
Reply With Quote
  #10 (permalink)  
Old 08-15-2007
Toby A Inkster
 
Posts: n/a
Default Re: How can I tell if a time was yesterday?

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/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:11 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0