Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Monday, June 27, 2016

Get / display all dates between two dates-php

It's very common uses to get all dates between two dates rang.

However php has a own class callled "DatePeriod" class which made our work easy for this task.


 $begin = new DateTime('2016-06-01');  
 $end = new DateTime('2016-06-28');  
 $end = $end->modify( '+1 day' );   
 $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);  
 foreach($daterange as $date){  
   echo $date->format("Y-m-d") . "<br>";  
 }  

Note :
 //(P1D stands for period of one day, see DateInterval for further documentation)
 //$end = $end->modify( '+1 day' ); mean dates include the last day 2016-06-28  

Output will looks like
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-05

Monday, April 18, 2016

Time Into Timestamp Or Timestamp Into Time In PHP

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.
NOTE : Timestamps in MariaDB/MYSQL have a maximum value of 2147483647, equivalent to 2038-01-19 05:14:07. This is due to the underlying 32-bit limitation. Using the function on a date beyond this will result in NULL being returned. 
So It's a good option to use other way like this simple php function.
The first problem is trying to convert a timestamp into a date. So here is a PHP function that does this.




// 1460865605 -> 2016-04-17 00:00:05

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

// Any Date format to Timestamp
echo(strtotime("now") . "<br>");  // 1461001723
echo(strtotime("2016-04-17") . "<br>"); // 1460865600
echo(strtotime("2016-04-17 00:00:05") . "<br>"); //  1460865605
echo(strtotime("16 October 2016") . "<br>");  //  1476590400
echo(strtotime("+5 hours") . "<br>");  //  1461019723
echo(strtotime("+1 week") . "<br>");  //  1461606523
echo(strtotime("+1 week 3 days 7 hours 5 seconds")."<br>");//1461890928
echo(strtotime("next Monday") . "<br>");  //1461556800
echo(strtotime("last Sunday")); // 1460865600

Convert different date time format functions

function return_date($date="") {
  if (!empty($date)) {
    return date("d-m-Y",strtotime($date));
  } else {
    return  "";
  }  
}
//  
function return_time($datetime="") {
  if (!empty($datetime)) {
    return date("d-m-Y h:i A",strtotime($datetime));
  } else {
    return  "";
  }  
}

function convert_date($date="") {
  if (!empty($date)) {
    return date("Y-m-d",strtotime($date));
  } else {
    return  "";
  }  
}

function convert_time($datetime="") {
  if (!empty($datetime)) {
    return date("Y-m-d H:i:s",strtotime($datetime));
  } else {
    return  "";
  }  
}
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Total Pageviews