Wednesday 24 February 2016

PHP mp3 Audio file upload

<?php
/*
ini_set("error_display",1);

ini_set('memory_limit', '32M');
ini_set('upload_max_filesize', '24M');
ini_set('post_max_size', '32M');

//echo ini_get('upload_max_filesize');
*/

$uploadpath = 'upload/';      // directory to store the uploaded files
$max_size = 30000;          // maximum file size, in KiloBytes
$alwidth = 900;            // maximum allowed width, in pixels
$alheight = 800;           // maximum allowed height, in pixels
$allowtype = array('wav', 'mp3');        // allowed extensions

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
// print_r($_FILES['fileup']);
// exit();
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));
  $type = end($sepext);       // gets extension
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';         // to store the errors

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\\\/').'/'.$uploadpath.'</b>';
    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}
?> 
<div style="margin:1em auto; width:333px; text-align:center;">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
  Upload File: <input type="file" name="fileup" /><br/>
  <input type="submit" name='submit' value="Upload" /> 
 </form>
</div>


PHP MYSQL - DATE and TIME functions


TIMEDIFF( )

Synopsis

TIMEDIFF(time, time)
This function returns the time difference between the two times given. Although the arguments may be given in time or date-and-time format, both arguments must be of the same datatype. This function is available as of Version 4.1.1 of MySQL.
SELECT appointment AS Appointment, NOW( ) AS Now,
       TIMEDIFF(appointment, NOW( )) AS 'Time Remaining'
   FROM appointments
   WHERE rec_id='3783';
+--------------------+--------------------+----------------+
| Appointment        | Now                | Time Remaining |
+--------------------+--------------------+----------------+
| 2005-01-11 10:30:00| 2005-01-11 22:28:09| 12:01:51       |
+--------------------+--------------------+----------------+
TIME( )

Synopsis

TIME(time)
This function returns the time from a given string or column containing date and time data. This function is available as of Version 4.1.1 of MySQL.
SELECT TIME(NOW( )) , NOW( );
+-------------+---------------------+
| TIME(NOW( )) | NOW( )                |
+-------------+---------------------+
| 21:14:20    | 2005-01-11 21:14:20 |
+-------------+---------------------+
SYSDATE( )

Synopsis

SYSDATE( )
This function returns the system date. It will return the date and time in ayyyy-mm-dd hh:mm:ss format, but will return the data in a yyyymmddformat if it's used as part of a numeric calculation. This function is an alias for the NOW( ) function.
SELECT SYSDATE( );
+---------------------+
| SYSDATE( )           |
+---------------------+
| 2004-05-09 18:44:51 |
+---------------------+
SUBTIME( )

Synopsis

SUBTIME(datetime, datetime)
This function returns the date and time for the given string or column, decreased by the time given as the second argument (d hh:mm:ss). If a negative number is given, the time is added and the function is the equivalent of ADDTIME(). This function is available as of Version 4.1.1 of MySQL.
SELECT NOW( ) AS Now,
       SUBTIME(NOW( ), '1:00:00.00') AS 'Hour Ago';
+---------------------+---------------------+
| Now                 | Hour Ago            |
+---------------------+---------------------+
| 2005-01-12 00:54:59 | 2005-01-11 23:54:59 |
+---------------------+---------------------+
Notice that the hour was decreased by one, and because the time is just after midnight, the function causes the date to be altered by one day, as well. To decrease the date, add the number of days before the time (separated by a space) like so:
 SELECT NOW( ) AS Now,
        SUBTIME(NOW( ), '30 0:0.0') AS 'Thirty Days Ago';
+---------------------+---------------------+
| Now                 | Thirty Days Ago     |
+---------------------+---------------------+
| 2005-01-12 00:57:04 | 2004-12-13 00:57:04 |
+---------------------+---------------------+
SUBDATE( )

Synopsis

SUBDATE(date, INTERVAL value type)
Use this function to subtract a time interval from the results of a date or timedatatype column. If a negative value is given, the interval is added and is equivalent to the ADDDATE( ) function. This is an alias for the DATE_SUB( )function. See DATE_ADD( ) for a table of incremental types.
SELECT SUBDATE(NOW( ), INTERVAL 1 DAY)
          AS 'Yesterday',
       SUBDATE(NOW( ), INTERVAL -1 DAY)
          AS 'Tomorrow';
+---------------------+---------------------+
| Yesterday           | Tomorrow            |
+---------------------+---------------------+
| 2004-05-09 16:11:56 | 2004-05-11 16:11:56 |
+---------------------+---------------------+
As of Version 4.1 of MySQL, for subtracting days the second argument of the function may simply be the number of days (i.e., just 1 instead of INTERVAL 1DAY).

TIMESTAMPDIFF( )

TIMESTAMPDIFF(interval, datetime, datetime)
This function returns the time difference between the two times given but only for the interval being compared. The intervals accepted are the same as those for the TIMESTAMPADD( ) function. This function is available as of Version 5 of MySQL.
SELECT NOW( ) AS Today,
       TIMESTAMPDIFF(DAY, NOW( ), LAST_DAY(NOW( )))
          AS 'Days Remaining in Month';
+---------------------+-------------------------+
| Today               | Days Remaining in Month |
+---------------------+-------------------------+
| 2016-02-24 07:03:41 | 4                       |
+---------------------+-------------------------+
TIMESTAMP( )

Synopsis

TIMESTAMP(date, time)
This function returns date and time (in yyyy-mm-dd hh:mm:ss format) from a given string or column containing date and time data, respectively. If only the date or only the time is given, the function will return zeros for the missing parameters. This function is available as of Version 4.1.1 of MySQL.
SELECT TIMESTAMP(appt_date, appt_time)
   FROM appointments LIMIT 1;
+---------------------------------+
| TIMESTAMP(appt_date, appt_time) |
+---------------------------------+
| 2005-01-15 10:30:00             |
+---------------------------------+

uniqid() - Four ways to generate unique id by PHP

 Four ways to generate unique id by PHP


1. Using uniqid() function

<?php
//creates a unique id with the 'about' prefix$a uniqid(about);
echo 
$a;
echo 
"<br>";
//creates a longer unique id with the 'about' prefix$b uniqid (abouttrue);
echo 
$b;
echo 
"<br>";
//creates a unique ID with a random number as a prefix - more secure than a static prefix $c uniqid (rand(), true);
echo 
$c;
echo 
"<br>";
//this md5 encrypts the username from above, so its ready to be stored in your database$md5c md5($c);
echo 
$md5c;
echo 
"<br>";
?>

2. Using current time + IP style

<?php
//You can also use $stamp = strtotime ("now"); But I think date("Ymdhis") is easier to understand.$stamp date("Ymdhis");$ip $_SERVER['REMOTE_ADDR'];$orderid "$stamp-$ip";$orderid str_replace(".""""$orderid");
echo(
$orderid);
echo 
"<br>";
?>

3. Generate custom length unique id

<?php
//set the random id length $random_id_length 10;
//generate a random id encrypt it and store it in $rnd_id $rnd_id crypt(uniqid(rand(),1));
//to remove any slashes that might have come $rnd_id strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string $rnd_id str_replace(".","",$rnd_id); $rnd_id strrev(str_replace("/","",$rnd_id));
//finally I take the first 10 characters from the $rnd_id $rnd_id substr($rnd_id,0,$random_id_length);

echo 
"Random Id: $rnd_id" ;
echo 
"<br>";
?>

4. Generate XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id, (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)

<?php
// Generate Guid function NewGuid() {
    
$s strtoupper(md5(uniqid(rand(),true)));
    
$guidText =
        
substr($s,0,8) . '-' .
        
substr($s,8,4) . '-' .
        
substr($s,12,4). '-' .
        
substr($s,16,4). '-' .
        
substr($s,20);
    return 
$guidText;
}
// End Generate Guid
$Guid NewGuid();
echo 
$Guid;
echo 
"<br>";
?>

Friday 5 February 2016

PHP Get last 12 months from now

<?php
$months = array();
for ($i = 1; $i <= 12; $i++) {
    $months[] = date("Ym", strtotime( date( 'Y-m-01' )." -$i months"));
}
print_r($months);
?>