Archive

Posts Tagged ‘convert’

Convert Time Into Timestamp Or Timestamp Into Time

May 10th, 2008 1 comment

The easiest (and most reliable) way to store the time in a database table is with a timestamp. It is also the most convenient way of working out time scales as you don’t have to do calculations in base 60. In MySQL this is accomplished by the UNIXTIME() function, which can be reversed by using another MySQL function called FROM_UNIXTIME().

However, you can sometimes be left with timestamps in your code and the task of trying to figure out what to do with them.

The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.

function timestamp($t = null){
 if($t == null){
  $t = time();
 }
 return date('Y-m-d H:i:s', $t);
}

And if you ever have a the opposite problem then here is a PHP function that converts a date string into a timestamp. At the moment the string needs to be in the format YYYY-MM-DD HH:MM:SS, which is what the previous function produced. This isn’t too difficult to change, just alter the parameters and order of the explode(‘ ‘, $str).

function convert_datetime($str) {
 
 list($date, $time) = explode(' ', $str);
 list($year, $month, $day) = explode('-', $date);
 list($hour, $minute, $second) = explode(':', $time);
 
 $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
 
 return $timestamp;
}

Here is an example of the functions in use.

echo timestamp(convert_datetime('2008-05-10 20:56:00')). ' '. convert_datetime('2008-05-10 20:56:00') . ' 1210467360';

Categories: PHP Tags: , , , ,

Convert HTML To ASCII With PHP

April 26th, 2008 2 comments

The reverse of turning ASCII text into HTML is to convert HTML into ASCII. And to this end here is a little function that does this.

function html2ascii($s){
 // convert links
 $s = preg_replace('/<a\s+.*?href="?([^\" >]*)"?[^>]*>(.*?)<\/a>/i','$2 ($1)',$s);
 
 // convert p, br and hr tags
 $s = preg_replace('@<(b|h)r[^>]*>@i',"\n",$s);
 $s = preg_replace('@<p[^>]*>@i',"\n\n",$s);
 $s = preg_replace('@<div[^>]*>(.*)</div>@i',"\n".'$1'."\n",$s);
 
 // convert bold and italic tags
 $s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s);
 $s = preg_replace('@<strong[^>]*>(.*?)</strong>@i','*$1*',$s);
 $s = preg_replace('@<i[^>]*>(.*?)</i>@i','_$1_',$s);
 $s = preg_replace('@<em[^>]*>(.*?)</em>@i','_$1_',$s);
 
 // decode any entities
 $s = strtr($s,array_flip(get_html_translation_table(HTML_ENTITIES)));
 
 // decode numbered entities
 $s = preg_replace('/&#(\d+);/e','chr(str_replace(";","",str_replace("&#","","$0")))',$s);
 
 // strip any remaining HTML tags
 $s = strip_tags($s);
 
 // return the string
 return $s;
}

To use this function just pass it a string. Here is an example of it at work.

$htmlString = '<p>This is some <strong>XHTML</strong> markup that <em>will</em> be<br />turned <a href="http://www.talkincode.com/" title="Talk in code">into</a> an ascii string</p>';
 
echo html2ascii($htmlString);

Produces the following output.

This is some *XHTML* markup that _will_ be
turned into (http://www.talkincode.com/) an ascii string

Categories: PHP Strings Tags: , , , ,

Turning ASCII Text Into HTML With PHP

April 25th, 2008 No comments

Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.

function ascii2html($s){
 $s = htmlentities($s);
 // try and split the text by a double line break
 $paragraphs = split("\n\n",$s);
 if(count($paragraphs)<2){
  // if there isn't enough array there then try and split it by single
  $paragraphs = split("\n",$s);
 };
 for($i = 0,$j = count($paragraphs);$i < $j;$i++){
  // create links around URLs
  $paragraphs[$i] = preg_replace('/((ht|f)tp:\/\/[^\s&]+)/','<a href="$1">$1</a>',$paragraphs[$i]);
  // create links around email addresses
  $paragraphs[$i] = preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i','<a href="mailto:$0">$0</a>',$paragraphs[$i]);
  // make paragraph
  $paragraphs[$i] = '<p>'.$paragraphs[$i].'</p>';
 };
 // join all paragraphs and return
 return join("\n",$paragraphs);
}

To test this use the following text sample.

$text = "this is some text
that splits across several
lines and
has some links like this
one here http://www.talkincode.com which
will be used to create a bunch of html";

And call the ascii2html() function like this.

echo ascii2html($text);

This will produce the following output.

<p>this is some text</p>
<p>that splits across several</p>
<p>lines and</p>
<p>has some links like this </p>
<p>one here <a href="http://www.talkincode.com">http://www.talkincode.com</a> which</p>
<p>will be used to create a bunch of html</p>

It might be prudent to use the strip_tags() function to clean the ASCII text before you use this function as it might lead to invalid HTML.

Categories: PHP Strings Tags: , , , ,