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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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). |
|
|||
|
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 -- |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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:
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 |
|
|||
|
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). |
|
|||
|
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 |
|
|||
|
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/ |