Friday, 3 June 2016

To check a date is entered in YYYY-MM-DD format.

To check a date is entered in YYYY-MM-DD format.
<?php
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){ 
    //it's ok 
}else{ 
    //it's bad 
}

// another way
$datein = '1998-09-21';
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $datein)){
    echo 'go';
}else{
    echo 'no go';
}


//with function
function MyCheckDate( $postedDate ) {
   if ( ereg("^[0-9]{4}-[01][0-9]-[0-3][0-9]$",$postedDate) ) {
      list( $year , $month , $day ) = explode('-',$postedDate);
      return( checkdate( $month , $day , $year ) );
   } else {
      return( false );
   }
}

function MyCheckDate( $postedDate ) {
   if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
      list($year , $month , $day) = explode('-',$postedDate);
      return checkdate($month , $day , $year);
   } else {
      return false;
   }
} 


//
function MyCheckDate( $postedDate ) {
   if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
      list($year , $month , $day) = explode('-',$postedDate);
      return checkdate($month , $day , $year);
   } else {
      return false;
   }

//
function MyCheckDate( $postedDate ) {
   if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $postedDate, $datebit)) {
      return checkdate($datebit[2] , $datebit[3] , $datebit[1]);
   } else {
      return false;
   }
} 
  
 
 
 

?> 

How To Export MySQL Data Into CSV Or Excel Format

Logic of Script
  • Establish Connection
  • Select your Database
  • Fetching Data from the table
  • Get total no. of the fields using mysql_num_fields
  • Get the names of the fields through the loop using mysql_field_name
  • The header() function sends a raw HTTP header to a client.To know more about the options please refer below links
<?php

//database connection details
$connect = mysql_connect('localhost','root','');

//your database name
mysql_select_db('test',$connect);

//selecting records from the table
$query = "SELECT * FROM csvtbl limit 50";
$header = '';
$data ='';

$export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\nNo Record(s) Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

?>