Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Czas, odejmowanie czasu (timeuser)
magu112
post 22.07.2009, 22:01:49
Post #1





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Kiedyś napisałem skrypt który oblicza ile dany user jest czasu na stronie, i teraz chciałem go wykorzystać lecz nie działa poprawnie ;x
Przejrzałem jeszcze kod na szybko i sie niczego nie dopatrzyłem :|

Może wy coś poradzicie....

  1. <?php
  2. function czas () {
  3.    global $db;
  4.    $result = $db->query("SELECT reg_date, lastdate, user_id FROM " . PREFIX . "_users ORDER BY reg_date DESC LIMIT 0,1");
  5.    $row = $db->get_row($result);
  6.  
  7. $with_us = max(1, round( ( time() - $reg_date ) / 9999999999 ));
  8.                                          
  9. $count_days=1;
  10.  
  11. $r1="rok";
  12. $r2="lata";
  13. $r3="lat";
  14. $m1="miesiąc";
  15. $m2="miesiące";
  16. $m3="miesięcy";
  17. $d1="dzień";
  18. $d2="dni";
  19.  
  20. $with_us_year = floor($with_us / 365);
  21.  
  22. if ($with_us_year >= 1)
  23. {
  24. if ($with_us_year == 1) $after_y = $r1;
  25. if ($with_us_year > 1 && $with_us_year < 5) $after_y = $r2;
  26. if ($with_us_year > 4) $after_y = $r3;
  27. $years = $with_us_year." ".$after_y;
  28. $with_us = $with_us - ($years * 365);
  29. $count_days=0;
  30. }
  31. $with_us_month = floor($with_us / 31);
  32. if ($with_us_month >= 1)
  33. {
  34. if ($with_us_month == 1) $after_m = $m1;
  35. if ($with_us_month > 1 && $with_us_month < 5) $after_m = $m2;
  36. if ($with_us_month > 4) $after_m = $m3;
  37. $monthes = $with_us_month." ".$after_m;
  38. $with_us = $with_us - ($monthes * 31);
  39. if ($with_us_month > 6) $count_days=0;
  40. if ($with_us_month == 12) {$years = ($with_us_year+1)." ".$r1;$with_us_month=$monthes=''; }
  41. }
  42. if ($count_days == 1)
  43. {
  44. if ($with_us > 0)
  45. {
  46. if ($with_us == 1) $after_d = $d1;
  47. if ($with_us > 1) $after_d = $d2;
  48. $days = $with_us." ".$after_d;
  49. }
  50. }
  51. $czas = $years." ".$monthes." ".$days;
  52. unset($years); unset($monthes); unset($days);
  53. return $czas;
  54. }
  55. $with_us = czas (reg_date);
  56. ?>


Ten post edytował magu112 23.07.2009, 17:11:13
Go to the top of the page
+Quote Post
Darti
post 22.07.2009, 22:03:47
Post #2





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


a co konkretnie nie działa ?


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 22.07.2009, 22:26:13
Post #3





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


cały czas pokazuje "1 dzień"

Ten post edytował magu112 22.07.2009, 22:35:23
Go to the top of the page
+Quote Post
Darti
post 22.07.2009, 22:39:51
Post #4





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


spróbuj w 9 linii dać
  1. <?php
  2. //$with_us = max(1, round( ( time() - $reg_date ) / 9999999999 ));
  3. $with_us = time() - $reg_date;
  4. ?>


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 22.07.2009, 22:52:45
Post #5





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


próbowałem i tak lecz wtedy wychodzi "3419998 lat 7 miesięcy "
Go to the top of the page
+Quote Post
Darti
post 22.07.2009, 22:59:10
Post #6





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


a wiesz o tym że $reg_date ma byc iloscia sekund (vide unix_timestamp) ?
sprawdź czy faktycznie tak jest ...


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 09:03:25
Post #7





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Nie no, bez przesady aż tak ciemny to ja nie jestem, zresztą pisałem ten skrypt. smile.gif
Aktualnie mam wartość "1248011970" przeliczając wyjdzie "19 Lipiec 2009 15:59" a skrypt ma na zasadzie obliczyć (zaokrąglając to do pełnej liczby) ile user jest na stronie.

Ten post edytował magu112 23.07.2009, 09:04:39
Go to the top of the page
+Quote Post
Spawnm
post 23.07.2009, 09:07:45
Post #8





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Proszę poprawić tytuł na bardziej sensowny.
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 17:11:37
Post #9





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Pomoże ktoś, i powie co jest źle ? ? ;x

Ten post edytował magu112 23.07.2009, 17:12:02
Go to the top of the page
+Quote Post
Darti
post 23.07.2009, 17:29:12
Post #10





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


Całość jest źle, założenia są złe ...


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 17:48:14
Post #11





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Dobra, dzięki za wielką pomoc... sad.gif
Go to the top of the page
+Quote Post
Darti
post 23.07.2009, 18:17:30
Post #12





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


  1. <?php
  2. function czas ($reg_date=0) {
  3.    if(!$reg_date){
  4.        global $db;
  5.        $result = $db->query("SELECT reg_date, lastdate, user_id FROM " . PREFIX . "_users ORDER BY reg_date DESC LIMIT 0,1");
  6.        $row = $db->get_row($result);
  7.        $reg_date = $row->reg_date;    //nie wiem jak Ci baza to oddaje, moze być $row['reg_date'];
  8.    }
  9.    
  10.    $with_us = time() - $reg_date;
  11.    $count_days=1;
  12.  
  13.    $r1="rok";
  14.    $r2="lata";
  15.    $r3="lat";
  16.    $m1="miesiąc";
  17.    $m2="miesiące";
  18.    $m3="miesięcy";
  19.    $d1="dzień";
  20.    $d2="dni";
  21.  
  22.    $with_us_year = floor($with_us / 365/24/60/60);
  23.  
  24.    if ($with_us_year >= 1)
  25.    {
  26.        if ($with_us_year == 1) $after_y = $r1;
  27.        if ($with_us_year > 1 && $with_us_year < 5) $after_y = $r2;
  28.        if ($with_us_year > 4) $after_y = $r3;
  29.        $wu['years'] = $with_us_year." ".$after_y;
  30.        $with_us = $with_us - ($wu['years'] * 365*24*60*60);
  31.        $count_days=0;
  32.    }
  33.    $with_us_month = floor($with_us / 31/24/60/60);
  34.    if ($with_us_month >= 1)
  35.    {
  36.        if ($with_us_month == 1) $after_m = $m1;
  37.        if ($with_us_month > 1 && $with_us_month < 5) $after_m = $m2;
  38.        if ($with_us_month > 4) $after_m = $m3;
  39.        $wu['monthes'] = $with_us_month." ".$after_m;
  40.        $with_us = $with_us - ($wu['monthes'] * 31*24*60*60);
  41.        if ($with_us_month > 6) $count_days=0;
  42.        if ($with_us_month == 12){
  43.            $years = ($with_us_year+1)." ".$r1;
  44.            $wu['monthes']='';
  45.        }
  46.    }
  47.    if ($count_days == 1)
  48.    {
  49.        $with_us_days = floor($with_us/24/60/60);
  50.        if ($with_us_days > 1)
  51.        {
  52.            if ($with_us_days == 1){
  53.                $after_d = $d1;
  54.            } else {
  55.                $after_d = $d2;
  56.            }            
  57.            $wu['days'] = $with_us_days." ".$after_d;
  58.        }
  59.    }
  60.    $czas = implode(' ',$wu);
  61.    unset($wu);
  62.    return $czas;
  63. }
  64.  
  65. echo czas (mktime(0,0,0,5,1,2005));
  66. echo czas ();
  67. ?>



// Pomógł++
// ayeo


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 18:46:22
Post #13





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


To samo jak zmieniłem w 7 linijce w swojej wersji kodu żeby sekundy przeliczał na dni czyli;
  1. <?php
  2. $with_us = max(1, round( ( time() - $reg_date ) / 86400 ));
  3. ?>


Twój kod jest inaczej napisany, w tym że pokazuje identyczny wynik, czyli "39 lat 6 miesięcy" zamiast 4 dni ;x
Go to the top of the page
+Quote Post
Darti
post 23.07.2009, 18:55:15
Post #14





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


bo $with_us daję w sekundach a nie w dniach ..... jak skopiujesz i wkleisz ode mnie to będzie dzałało


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 19:01:27
Post #15





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Napisałem że działa tylko pokazuje taki sam, zły wynik jak i w moim skrypcie, czyli "39 lat 6 miesięcy" zamiast 4 dni ;x...
Go to the top of the page
+Quote Post
Darti
post 23.07.2009, 19:10:46
Post #16





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


to znaczy że coś w bazie masz źle zapisane.
Jak wywołasz to tak :
  1. <?php
  2. echo czas (mktime(0,0,0,7,19,2009));
  3. ?>

To będziesz miał 4 dni


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
magu112
post 23.07.2009, 19:16:39
Post #17





Grupa: Zarejestrowani
Postów: 47
Pomógł: 2
Dołączył: 22.07.2009

Ostrzeżenie: (0%)
-----


Wtedy wychodzi "4 lata 2 miesiąc"

///

A w bazie danych wszystko jest dobrze, mam wartość 1248011970

Ten post edytował magu112 23.07.2009, 19:17:21
Go to the top of the page
+Quote Post
Darti
post 23.07.2009, 19:23:58
Post #18





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


Wywołując moją funkcję z wartością 1248011970 też dostaję 4 dni. coś nie tak wyciągasz z tej bazy.
Wklej jeszcze raz jak masz teraz.

Ten post edytował Darti 23.07.2009, 19:25:32


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
Victor152
post 23.07.2009, 23:51:32
Post #19





Grupa: Zarejestrowani
Postów: 105
Pomógł: 3
Dołączył: 26.02.2008

Ostrzeżenie: (0%)
-----


Albo ma dupną datę na serwerze.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 5.07.2025 - 06:26