Archive for the ‘php’ Category

PHP Delete file

Delete a file in the file system. Note that this file system we’re talking about is the server’s file system. PHP nor other web-based programming language deletes a file system on the client’s side.

function deleteFile($filename) {
  $do = unlink($filename);

  if ($do=="1") {
    echo "The file was deleted successfully.";
  } else {
    echo "There was an error trying to delete the file.";
  }
}

Found this useful? Donations appreciated to help keep this blog alive.

PHP Get difference between 2 dates

This code gets the date difference manually, specifying a date value.

$digest_date = "2009-12-25";
$date_diff = abs(strtotime(date('y-m-d'))-strtotime($digest_date)) / 86400;

and another version in a method if you want to keep on reusing this code.

function dateDiff($dformat, $endDate, $beginDate) {
  $date_parts1=explode($dformat, $beginDate);
  $date_parts2=explode($dformat, $endDate);
  $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
  $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
  return $end_date - $start_date;
}

Found this useful? Donations appreciated to help keep this blog alive.

PHP: What’s the deal with strpos() ?

Languages like Java and Javascript provide a method called indexOf() that returns an int of the first position of the string key found in a string. strpos() in PHP acts differently. If it finds the substring within the string, it returns an int value of the position of the substring. If it does not, it returns a boolean instead.

I find this stupid. It would have been easier if it still returned an int value like the usual -1 to indicate it didn’t find any substring inside the string.

To do an if statement using strpos() of PHP just like this Java code:

Do this in PHP:

Notice the 2 equal sign succeeding after the exclamation point? That line checks if the statement is false. If you wish to change it to a true condition statement, just put in 3 equal signs.

Found this useful? Donations appreciated to help keep this blog alive.

Related Posts Plugin for WordPress, Blogger...