Monday, 12 January 2015

PHP Concatenation – Comma (,) vs Dot (.)

Comma isn’t a concatenator. it use for printing or rather echoing a list of variable, string and numbers similar to how it is used in English. While dot joins two strings together to make one longer string.
Take note: comma only work when using the echo language construct and you can’t return a comma delimited variable.
Examples
Let’s see some code example.
  • ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    echo $a, $b;
    ?
    1
    2
    3
    4
    5
    <?php
    $a = 'i am';
    $b = ' boy';
    $c = $a . $b;
    echo $c;
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    echo $a . $b;
    Result: i am boy
  • ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    print($a, $b);
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    $c = $a, $b;
    ?
    1
    2
    3
    4
    <?php
    $a = 'i am';
    $b = ' boy';
    return $a , $b;
    Result: Parse error: syntax error, unexpected ‘,’ in C:\xampp\htdocs\index.php on line 4
It is worth noting that when using echo, delimiting string with comma is faster than dot.

Thursday, 8 January 2015

How to get next day, last day and next monday like dates in PHP

<?php
function formatNextMondayFromNow( DateTime $dt )
{
        return $dt->modify( 'next monday' )->format( 'Y-m-d' );
}

$d = new DateTime();
echo formatNextMondayFromNow( $d ), "\n";
echo $d->format( 'Y-m-d' ), "\n";

 echo "<br>--------------------------------------------<br>";
?>


<?php
function formatNextdayFromNow( DateTime $dt )
{
        return $dt->modify( 'next day' )->format( 'Y-m-d' );
}

$d = new DateTime();
echo formatNextdayFromNow( $d ), "\n";
echo $d->format( 'Y-m-d' ), "\n";

 echo "<br>--------------------------------------------<br>";
?>


<?php
function formatLastMondayFromNow( DateTime $dt )
{
        return $dt->modify( 'last monday' )->format( 'Y-m-d' );
}

$d = new DateTime();
echo formatNextMondayFromNow( $d ), "\n";
echo $d->format( 'Y-m-d' ), "\n";

 echo "<br>--------------------------------------------<br>";
?>


<?php
function formatLastdayFromNow( DateTime $dt )
{
        return $dt->modify( 'last day' )->format( 'Y-m-d' );
}

$d = new DateTime();
echo formatLastdayFromNow( $d ), "\n";
echo $d->format( 'Y-m-d' ), "\n";

 echo "<br>--------------------------------------------<br>";
?>
 

Some brief explanation and it is very important.

The first time that my improved DateTime support made its way into PHP was officially in PHP 5.1, although the more advanced features such as the DateTime class only made it appearance in PHP 5.2. Since its introduction the DateTime class implementation suffered from one design mistake — arguably not something that even an RFC would have highlighted.
In PHP, if you do the following:

<?php
function formatNextMondayFromNow( DateTime $dt )
{
        return $dt->modify( 'next monday' )->format( 'Y-m-d' );
}

$d = new DateTime();
echo formatNextMondayFromNow( $d ), "\n";
echo $d->format( 'Y-m-d' ), "\n";
?>

It displays:

2014-02-17
2014-02-17

The modify() method does not only return the modified DateTime object, but also changes the DateTime object it was called on. In an API like above, this is of course totally unexpected and the only way to avoid this behaviour is to do the following instead:

echo formatNextMondayFromNow( clone $d ), "\n"; 
 
This mutability property that all modifying methods of the DateTime class have is highly annoying, and something that I would now rather remove. But of course we cannot as that would break backwards compatibility.

So in PHP 5.5, after a few stumbles, I finally managed to rectify this. I did not change the original class's behaviour, but instead, I added a new class. The new DateTimeImmutable class which does not display this "mutable" behaviour, and only returns the modified object:
<?php 
function formatNextMondayFromNow( DateTimeImmutable $dt ) { 
 return $dt->modify( 'next monday' )->format( 'Y-m-d' ); 
}
$d = new DateTimeImmutable();
 echo formatNextMondayFromNow( $d ), "<br>";
 echo $d->format( 'Y-m-d' ), "\n"; 
?>
 
 

Obtaining the next month in PHP

Obtaining the next month in PHP

Over and over again PHP users complain that next month in PHP's date-string parser doesn't go to the next month, but instead skips to the one after next month; like in the following example:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>

The output of the little script will be March. March obviously doesn't follow January as February is in between. However, the current behavior is correct. The following happens internally:
  • next month increases the month number (originally 1) by one. This makes the date 2010-02-31.
  • The second month (February) only has 28 days in 2010, so PHP auto-corrects this by just continuing to count days from February 1st. You then end up at March 3rd.
  • The formatting strips off the year and day, resulting in the output March.
This can easily be seen when echoing the date with a full date format, which will output March 3rd, 2010:
<?php
echo $d->format( 'F jS, Y' ), "\n";
?>

To obtain the correct behavior, you can use some of PHP 5.3's new functionality that introduces the relative time stanza first day of. This stanza can be used in combination with next month, fifth month or +8 months to go to the first day of the specified month. Instead of next month from the previous example, we use first day of next month here:
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'first day of next month' );
echo $d->format( 'F' ), "\n";
?>

This script will correctly output February. The following things happen when PHP processes this first day of next month stanza:
  • next month increases the month number (originally 1) by one. This makes the date 2010-02-31.
  • first day of sets the day number to 1, resulting in the date 2010-02-01.
  • The formatting strips off the year and day, resulting in the output February.
Besides first day of, there is an equivalent last day of to go to the last day of a month. The following example demonstrates this:
<?php
$d = new DateTime( '2010-01-08' );
$d->modify( 'last day of next month' );
echo $d->format( 'F jS, Y' ), "\n";
?>

This outputs February 28th, 2010. Internally the following happens:
  • next month increases the month number (originally 1) by one. This makes the date 2010-02-08.
  • last day of increases the month number by one, and sets the day number to 0, resulting in the date 2010-03-00.
  • PHP then auto-corrects the invalid day number 0 by removing one from the month and skipping to the last day of that month, resulting in 2010-02-28.

PHP - How to find the last day of the month from date?

<?php
//For finding month last date, modify as follows, 
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');  //O/P    2009-11-30
echo "<br>--------------------------------------------<br>";
//For finding next month last date, modify as follows, 
 $date->modify('last day of 1 month');
 echo $date->format('Y-m-d');   //O/P 2009-12-31
 
 echo "<br>--------------------------------------------<br>";
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
//O/P
/*--------------------------------------------
Sun, Jan 4th 2015
Sun, Jan 11th 2015
Thu, Jan 1st 2015
Sat, Jan 31st 2015
Thu, Jan 1st 2015
Thu, Dec 31st 2015 */
 
//Last day of every month in the current year
 
function lastDateOfMonth($Month, $Year=-1) {
    if ($Year < 0) $Year = 0+date("Y");
    $aMonth         = mktime(0, 0, 0, $Month, 1, $Year);
    $NumOfDay       = 0+date("t", $aMonth);
    $LastDayOfMonth = mktime(0, 0, 0, $Month, $NumOfDay, $Year);
    return $LastDayOfMonth;
}

for($Month = 1; $Month <= 12; $Month++)
    echo date("Y-n-j", lastDateOfMonth($Month))."\n";  
//for 2015
/*
2015-1-31
2015-2-28
2015-3-31
2015-4-30
2015-5-31
2015-6-30
2015-7-31
2015-8-31
2015-9-30
2015-10-31
2015-11-30
2015-12-31 
*/ 

?>

cal_days_in_month - PHP



cal_days_in_month — Return the number of days in a month for a given year and calendar
Description
int cal_days_in_month ( int $calendar , int $month , int $year )

This function will return the number of days in the month of year for the specified calendar.
Parameters calendar

Calendar to use for calculation month

Month in the selected calendar year

Year in the selected calendar
Return Values

The length in days of the selected month in the given calenda.

<?php
$d=cal_days_in_month(CAL_GREGORIAN,2,1965);
echo "There was $d days in February 1965.<br>";

$d=cal_days_in_month(CAL_GREGORIAN,2,2004);
echo "There was $d days in February 2004.";
?>

O/P:

There was 28 days in February 1965.
There was 29 days in February 2004. 

0 or CAL_GREGORIAN - Gregorian Calendar
1 or CAL_JULIAN - Julian Calendar
2 or CAL_JEWISH - Jewish Calendar
3 or CAL_FRENCH - French Revolutionary Calendar
 

MYSQL-To create a new table based on another tables structure / constraints



To create a new table based on another tables structure / constraints use : CREATE TABLE new_table LIKE old_table; 

To copy the data across, if required, use INSERT INTO new_table SELECT * FROM old_table;

Beware of the notes on the LIKE option :
Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

CREATE TABLE new_table LIKE original_table; The copy is created using the same version of the table storage format as the original table. The SELECT privilege is required on the original table.

LIKE works only for base tables, not for views.

CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions.