Jump to content
php.lv forumi

Php And Mysql, Unix And Time


Smejmoon

Recommended Posts

Divi ērtākie veidi, ko atradu, lai ievietotu notikuma laiku datubāzē un tad no PHP attēlot cilvēkiem lasāmā formā.

 

Man nepatīk, ka ievietojot datus datubāzē, tie nav jāpārveido, bet izņemot ir.

 

Ja kā datu tipu nelietoju TIMESTAMP, bet INT, tad nevaru izmantot MySQL Data and Time functions.

 

<code><font color="#000000">

<font color="#0000CC"><? 
</font><font color="#FF9900">/* 
CREATE TABLE timeline ( 
  event_time timestamp(14) NOT NULL, 
) TYPE=MyISAM; 
*/ 

//ja dati tiek ievietoti izmantojot MySQL NOW() f-ju 

</font><font color="#0000CC">mysql_query </font><font color="#006600">(</font><font color="#CC0000">"INSERT INTO timeline (event_time) VALUES(NOW())"</font><font color="#006600">); 

</font><font color="#FF9900">//vai PHP time() 

</font><font color="#0000CC">$time </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/time]time[/url]</font><font color="#006600">(); 
</font><font color="#0000CC">mysql_query </font><font color="#006600">(</font><font color="#CC0000">"INSERT INTO timeline (event_time) VALUES('$time ')"</font><font color="#006600">); 

</font><font color="#FF9900">/*rezultaats buus vienaads abos veidos, kaa tiek ieguuti dati: 
 - izmantojot MySQL DATE_FORMAT() 
 - izmantojot MySQL UNIX_TIMESTAMP() un PHP strftime 
*/ 

</font><font color="#0000CC">$rows </font><font color="#006600">= </font><font color="#0000CC">query</font><font color="#006600">(</font><font color="#CC0000">" 
			 SELECT DATE_FORMAT(event_time,'%Y.%m.%d %H:%i:%s') as df 
			 , UNIX_TIMESTAMP(event_time) as ts 
			 FROM timeline 
			 "</font><font color="#006600">); 
for(;</font><font color="#0000CC">$row </font><font color="#006600">= </font><font color="#0000CC">getRow</font><font color="#006600">(</font><font color="#0000CC">$rows</font><font color="#006600">);) 
{ 
 echo </font><font color="#0000CC">$row</font><font color="#006600">-></font><font color="#0000CC">df</font><font color="#006600">; 
 echo </font><font color="#CC0000">" "</font><font color="#006600">; 
 echo </font><font color="#0000CC">strftime </font><font color="#006600">(</font><font color="#CC0000">"%Y.%m.%d %H:%M:%S"</font><font color="#006600">,</font><font color="#0000CC">$row</font><font color="#006600">-></font><font color="#0000CC">ts</font><font color="#006600">); 
 echo </font><font color="#CC0000">""</font><font color="#006600">; 
} 
</font><font color="#0000CC">?>
</font>
</font>
</code>

Source iekš paste.php.lv

Link to comment
Share on other sites

×
×
  • Create New...