ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 206
W CZYM MOGĘ POMÓC?


   
USŁUGI
Tworzenie/prowadzenie stron
Pisanie/modyfikacje skryptów
Systemy CMS, bazy danych
Szkolenia, doradztwo, audyty
Fotograf Warszawa
   
OPINIE UŻYTKOWNIKÓW
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!

Kamil Dmowski
Polski Czerwony Krzyż

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

strtotime

(PHP 3 >= 3.0.12, PHP 4, PHP 5)

strtotime --  Parsuje większość angielskich tekstowych opisów daty i czasu do uniksowego znacznika czasu

Opis

int strtotime ( string czas [, int teraz] )

Funkcja przyjmuje tekst zawierający datę w formacie angielskim i stara się przeliczyć ją na uniksowy znacznik czasu, relatywnie do znacznika czasu podanego w teraz, lub aktualnego czasu, jeśli znacznik nie zostanie podany. W przypadku fiaska, zwracane jest -1.

Ponieważ strtotime() zachowuje się zgodnie ze składnią daty wg GNU, warto zajrzeć do rozdziału podręcznika GNU zatytułowanego Formaty Wprowadzania Daty - Date Input Formats. Opisana tam jest poprawna składnia argumentu czas.

Przykład 1. przykłady strtotime()

<?php
echo strtotime ("now"), "\n";
echo
strtotime ("10 September 2000"), "\n";
echo
strtotime ("+1 day"), "\n";
echo
strtotime ("+1 week"), "\n";
echo
strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo
strtotime ("next Thursday"), "\n";
echo
strtotime ("last Monday"), "\n";
?>

Przykład 2. Checking for failure

<?php
$str
= 'Not Good';
if ((
$timestamp = strtotime($str)) === -1) {
   echo
"Tekst daty ($str) jest niepoprawny";
} else {
   echo
"$str == ". date('l dS of F Y h:i:s A',$timestamp);
}
?>

Notatka: Poprawny zakres znacznika czasu to zwykle od piątku, 13 grudnia 1901 20:45:54 GMT (czasu Greenwich) do wtorku, 19 stycznia 2038 03:14:07 GMT. (Wartości te odpowiadają minimalnej i maksymalnej wartości 32-bitowej liczbie całkowitej ze znakiem).




User Contributed Notes

Paul Wib
26-Jan-2006 02:37

Slight tweak to ScottB's function for UK dates below. Accounts for when a year isn't provided:

function strtotime_uk($str)
{
   $str = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]*([0-9]{0,4})/", "\\2/\\1/\\3", $str);
   $str = trim($str,'/');
   echo "Converted to UK date: $str<br>";
   return strtotime($str);
}

As strtotime, if the year isn't provided it will default to current year:

strtotime_uk("13/11"); // 13th November, UK-style


kturner at kgt dot net
24-Jan-2006 07:18

It looks like in the latest release of PHP 5.1, when passing to strtotime this string "12/32/2005", it will now return the date "12/31/1969". (The previous versions would return "1/1/2006".)


matt at australiangamer dot com
18-Jan-2006 01:32

Regarding the previous post on strtotime() being used to convert mysql datetime strings, I agree that this is not widely realized.

Part of the reason for this is that it is only a relatively recent addition. Prior to PHP 5.0, though it might have been an earlier version, strtotime() would return -1 on a mysql datetime string.

If you're on a server that doesn't have a recent PHP version test your result before assuming this one works.


jeremy at citiesunlimited dot com
11-Jan-2006 06:03

There doesn't seem to be enough documentation about this feature of strtotime(), but it can take a SQL datetime column and convert it to a unix timestamp.

For example:
<?php

$timestamp
= strtotime('2006-01-10 16:09:30');
echo(
$timestamp);

// outputs: 1136930970
?>

This is useful with the Date() Function:
<?php

$timestamp
= strtotime('2006-01-10 16:09:30');
echo
date('n/j/Y g:i:sa', $timestamp);

// outputs: 1/10/2006 4:09:30pm
?>


shaver at qspeed dot com
11-Jan-2006 05:13

I'm posting these here as I believe these to be design changes, not bugs.

For those upgrading from PHP 4 to PHP 5 there are a number of things that are different about strtotime that I have NOT seen documented elsewhere, or at least not as clearly. I confirmed these with two separate fresh installations of PHP 4.4.1 and PHP 5.1.1.

1) Given that today is Tuesday: PHP4 "next tuesday" will return today. PHP5 "next tuesday" will actually return next tuesday as in "today +1week". Note that behavior has NOT changed for "last" and "this". For the string "last tuesday" both PHP4 and PHP5 would return "today -1week".  For the string "this tuesday" both PHP4 and PHP5 would return "today".

2) You cannot include a space immediately following a + or - in PHP 5. In PHP4 the string "today + 1 week" works great. in PHP5 the string must be "today +1 week" to correctly parse.

3) (Partially noted in changelog.) If you pass php4 a string that is a mess ("asdf1234") it will return -1. If you in turn pass this to date() you'll get a warning like: Windows does not support dates prior to midnight. This is pretty useful for catching errors in your scripts. In PHP 5 strtotime will return FALSE which causes date() to return 12/31/69. Note that this is true of strings that might appear right such as "two weeks".

4) (Partially noted in changelog.) If you pass php4 an empty string it will error out with a "Notice: strtotime(): Called with empty time parameter". PHP5 will give no notice and return the current date stamp. (A much preferred behavior IMO.)

5) Some uppercase and mixed-case strings no longer parse correctly. In php4 "Yesterday" would parse correctly. In php5 "Yesterday" will return the infamous 1969 date. This is also true of Tomorrow and Today.

6. The keyword "previous" is supported in PHP5. (Finally!)

Good luck with your upgrades. :)
 -Will


Mr Obvious
10-Jan-2006 03:10

@ nick at fortawesome dot co dot uk
regarding 'month' vs 'next month'

Not sure what version of PHP you found those results on, however with PHP 4.4.0,
<?PHP
$time1
=strtotime('next month');
$time2=strtotime('month');
?>
produce the exact same result.


dean at earlsoft dot co dot uk
01-Jan-2006 09:40

I have recently had to handle parsing date strings without a year, which broke when the year rolled over.

Assuming the dates will always be in the past, you can use this to fall back to the previous year if the date is too far in the future:

<?php
$data
= strtotime("26 dec");
print
date("r", $data) . "\n";
if (
$data > strtotime("+1 week")) {
  print
"Too far in the future\n";
 
$data = strtotime("-1 year", $data);
}
print
date("r", $data) . "\n";
?>

This outputs:
Tue, 26 Dec 2006 00:00:00 +0000
Too far in the future
Mon, 26 Dec 2005 00:00:00 +0000


rajbirsingh_83 at rediffmail dot com
21-Dec-2005 06:14

To add two timestamps and reflect thier effect on the date.

code:-

$t =strtotime('+1 hours 50 min 50 second',strtotime("2005-12-21 23:10:15"));
$result = date("Y-m-d H:i:s",$t);
echo $result;

Rajbir


16-Dec-2005 02:31

Note that by default strtotime( ) considers the string time like a local time.
If your input string time is GMT/UTC do :

<?php
   $date
= '2005-12-25 00:56:27 GMT' ; // Note the timezone specification
  
$time = strtotime($date) ;
   echo
date('d/m/Y H:i:s', $time) ; // $date will be correctly localized
   //

 

 
  © 1996-2010 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt