php convert a date to a timestamp - php

I'm trying to convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user
$today = date("d-m-y"); // 01-12-13
$start_date = "28-11-13";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Problem is that the result are incorrect
Result:
01-12-13 > 28-11-13 1008201600 > 1857686400
what's wrong ?

Always use four-digit years, as using two-digit years leads to endless headaches. 13 is not treated as a year; instead, 01-12-13 and 28-11-13 are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".
If you use 2013 instead, the issues disappear:
$today = date("d-m-Y"); // 01-12-2013
$start_date = "28-11-2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
Output:
01-12-2013 > 28-11-2013
1385884800 > 1385625600

My understanding of the documentation is that you are trying to convert a readable string into a Unix based timestamp. When you attempt to convert 01-12-13 and 28-11-13 you will get undesired results because the strtotime function has no recollection of how to properly interpret these values. Something along the lines of the code below should help you out.
$today = date("jS M Y"); // 1st Dec 2013
$start_date = "28th Nov 2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
output
1st Dec 2013 > 28th Nov 2013
1385877600 > 1385618400
Just adjust your code to fit within the limitations of the language and if these values are coming from a form field you should be able to quickly and easily convert them to a readable date.

The function assumes that you start with a year
(check : http://www.onlineconversion.com/unix_time.htm), change to
$start_date = "28-11-2013";
This way the function knows which is the year, it takes the default foramt which is (if not misstaken) Y-d-m

Related

Adding days to date, outputting incorrect dates? [duplicate]

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

Date in a URL dd/mm/yyyy

I am passing a date (dd/mm/yyyy) in URL with the following format:
http://www.website.com/_parameter=20/02/2000
I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$D->query = '';
if ($this->param('_parameter')) $D->query = date('Y-m-d', strtotime($this->param('_parameter')));
?>
And my database as following:
SELECT idtask FROM task WHERE unfinish=1 AND date LIKE '%".$D->query."%' "
The above return the following:
1970-01-01
When using strotime() you need to make sure you are using a valid datetime format. Otherwise strtotime() will return false or give you an unexpected value.
Dates that use XX/XX/XXXX format are assumed to be in US format which means the first two digits represent the month, the next two digits represent the day of month, and the last four digits represent the year. When dashes are used, the dates are assumed to be in European format. For example:
04/18/2017 = April 18, 2017
12/18/2017 = December 18, 2017
18-04-2017 = April 18, 2017
18-12-2017 = December 18, 2017
If you accidentally switch the day and month strtotime() will return false as the date is invalid.
18/04/2017 // error
18/12/2017 // error
04-18-2018 // error
12-18-2017 // error
The above examples are straight forward. But you can run into issues when the dates can be ambigous. For example:
04/12/2017 = April 12, 2017
12/04/2017 = December 4, 2017
04-12-2017 = December 4, 2017
12-04-2017 = April 12, 2017
In the above examples by switching the day and month we still get valid dates which can cause unexpected results in your application. To solve these potential issues it is recommended to use DateTime::createFromFormat() to parse the date ad return a DateTime() object from which you can get a Unix Timestamp, convert the date into another format, or use it to compare to other DateTime objects.
// Parse US date format
$date1 = DateTime::createFromFormat('m/d/Y', '04/18/2017');
// Get Unix timestamp of 1493581268
$timestamp = $date1->getTimestamp();
// Parse European date format
$date2 = DateTime::createFromFormat('d-m-Y', ''18-04-2017);
// Get MySQL format (ISO-8601) of 2017-04-18
$mysqlDate = $date2->format('Y-m-d');
See also:
Compare DateTime objects with comparison operators in PHP
For your specific case, the follow code will work:
$date = $date1 = DateTime::createFromFormat('m/d/Y', '20/02/2000');
$D->query = $date->format('Y-m-d'); // 2000-02-20

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

how to check two dates between from-date and to-date

I have two dates from-date & to-date.
I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking.
Example:
i have to check these dates:-> from=15 March 2013 & 15 April 2013 between the following dates whether any days falls in between these two date or not.
following data from db table
# from date to-date
-----------------------------------------
1 01 April 2013 30 April 2013 //here we will find as falling
2 01 May 2013 15 May 2013
3 01 June 2013 20 June 2013
Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..
The simplest way to compare dates is to convert them to a unix timestamp
Because the unix timestamp is an integer, you can simply use relational operators to compare them.
Example
// set some example data
$referenceDate = '01 April 2013';
$fromDate = '01 January 2013';
$toDate = '01 June 2013';
// convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );
// isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
EDIT 1
To actually answer your question:
You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?
// our two ranges overlap if the following conditions are met
$dateRangeOverlaps = $referenceFromTimestamp <= $toTimestamp and $fromTimestamp <= $referenceToTimestamp;
Please try the code,
$ourdate = strtotime('1 April 2013'); // Your date which is to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}
If you need to check several dates, pass it as an array, like below...
$i=0;
$dates= array("11 April 2013","16 April 2013");
foreach($dates as $ourdates)
{
$ourdate= strtotime($ourdates); //Your dates to be checked
$from = strtotime('15 March 2013'); // From date
$to = strtotime('15 April 2013'); // To date
if ($ourdate >= $from && $ourdate <= $to)
{
$i++;
}
}
if($i>0)
{
echo "Date falls";
}
else
{
echo "No Date falls";
}

How to return the a unix-timestamp for the start of a month of a particular unix-timestamp PHP

I need a generic function that takes a unix-timestamp representation of a date-time and returns a representation for, say, midnight on the first of the month.
i.e. I pass 1352376093 which represents now (Thu, 08 Nov 2012 12:01:33 GMT) and 1351728000 is returned which represents (Thu, 01 Nov 2012 00:00:00 GMT)
This snippet should help you to solve the problem. Just take only month and year from the existing date and convert it back to the timestamp.
<?php
$time = 1352376093;
$date = date('Y-m-01 00:00:00', $time);
$result = strtotime($date);
So ended up with this function:
$earliest_issue = db_result( $result, 0 );
$day_of_month_of_earliest_issue = date("d", $earliest_issue);
$hour_of_month_of_earliest_issue = date("h", $earliest_issue);
$minute_of_month_of_earliest_issue = date("i", $earliest_issue);
$seconds_in_month = date("s", $earliest_issue);
$seconds_in_day_of_month = (($day_of_month_of_earliest_issue*24*60*60)-86400);
$seconds_in_hour_of_month = (($hour_of_month_of_earliest_issue*60*60)-3600);
$seconds_in_minute_of_month = (($minute_of_month_of_earliest_issue*60));
$start_time_of_query = ($earliest_issue - $seconds_in_day_of_month - $seconds_in_hour_of_month - $seconds_in_minute_of_month - $seconds_in_month);
return $start_time_of_query;
where $earliest_issue is the start date...
If anyone reading this has a more elegant function as I had hoped then please contribute, thanks!

Categories