I want to calculate the no of days from a field where I enter the dates from calendar like 21 jan 2011, but when I use count() it will only count the whole string, how can I count the days?
I suggest you have a look at the DateTime::diff() method.
For example
$date1 = new \DateTime('21 Jan 2011');
$date2 = new \DateTime('28 Feb 2011');
$diff = $date1->diff($date2);
$days = $diff->d;
I presume your date is using the date object [ http://php.net/manual/en/function.date.php ]. If so, you could use something like this [ http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018-1024/ ] to calculate the difference between two dates. I presume that's what you're intending to do.
If you're not already using the date object, you can convert to the date object fairly easily. The tutorial linked to above demonstrates converting a string to a date using the explode function.
try this
echo date('d',strtotime('21 jan 2011'));
see this function for more strtotime
$date = "21 jan 2011";
$days = explode(" ", $date);
$days = $days[0];
Related
I'd like to use the datetime->modify function on a date string that's formatted like "21 Jan 2016". When I use the datetime->modify and add 1 day, it gives me a result of 30 Apr 2017. I know that if I don't use the short month name and use a number instead (i.e. 01), it will work fine but I would like to get it work this way with short month name. Is this possible?
Please see code below:
<?php
$date = "21 Jan 2016"; // this is my date string
$newdate = new DateTime($date );
$date2 = $newdate->modify('+1 day'); // add 1 day to date string
echo $date2->format("d-M-Y");
?>
RESULT is:
30-Apr-2017
RESULT WANTED
22-Jan-2016
The problem is that you are trying to create a DateTime object from a non-ISO format. That's that part that is not working.
Take a look at: http://php.net/manual/ro/datetime.createfromformat.php
You will need to have something like
DateTime::createFromFormat('d M Y', '21 Jan 2016');
Full example:
$tomorrow = DateTime::createFromFormat('d M Y', '21 Jan 2016')->modify('+1 day')->format("d-M-Y");
echo($tomorrow);
The format of the $date variable is incorrect. Off the top of my head, there are two easy ways to fix this:
Set $date = "Jan 21, 2016"
Set $date = "21-Jan 2016"
More options: https://secure.php.net/manual/en/datetime.formats.date.php
Your date format was wrong. That's all.
I am trying to calculate a date difference in days using MySQL and PHP date.
My code
$ArrivalDate = $variants_data['ArrivalDate'];
$daydiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24)));
Output
<td>'.$daydiff.'</td>
Results
I get 93 days instead of 26 days (got 26 days using this calculator http://easycalculation.com/date-day/number-of-days.php)
ArrivalDate value = 2013-05-03 from MySQL table and it changes due to transport delays, etc.
How can I achieve this in PHP?
try this
$daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
just change your current date function format so it will give your correct answer means 26 days.
Use DateTime class :
$today = new DateTime;
$oneWeekLater = clone $today;
$oneWeekLater->modify('+1 week');
$diff = $today->diff($oneWeekLater);
echo $diff->format('Y-m-d H:i:s');
http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.dateinterval.php
$diff = strtotime(date("d/m/Y")) - strtotime($ArrivalDate);
echo "Difference is $diff seconds\n";
$days = floor($diff/(3600*24));
echo "Difference is $days days\n";
You can also do it at the database level using the DATEDIFF() function.
http://www.w3schools.com/SQl/func_datediff_mysql.asp
$days = date("d", $timestamp1) - date("d", $timestamp2);
Try this
$ArrivalDate = '2013-05-03';
echo $daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
Your date function was not in proper format as that of arrival date
I have a month value (1-12), day value (1-31), and a year value (2010,2011,2012). I also have a hour value and a minute value.
How can I give this to strtotime() in a way it can convert it to a timestamp?
why convert string to date when you already know year month and date.
use setDate funtion
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
Given the variables $year $month $day $hour $minute you can do:
strtotime("$year-$month-$day $hour:$minute");
Be careful to enclose the variables with ", never in this case with '.
UPDATE (thanks to comments of #Clockwork and #Tadeck):
In case there is a variable $timeofday that represents the time of day (i.e. AM or PM),
then you can parse it this with:
strtotime("$year-$month-$day $hour:$minute$timeofday");
that is to say, just attach that variable to the end of the text.
Is strtotime the best tool for this job? What about mktime()?
$time = mktime($hour, $minute, 0, $month, $day, $year);
You can provide it to function strtotime() in many ways, as mentioned in documentation. Some examples include:
$your_time = strtotime('12/31/2011 9:59');
$your_time = strtotime('2011-12-31 9:59');
$your_time = strtotime('December 31, 2011 9:59');
etc. It really is very flexible.
You can find the list of valid formats in the documentation, and that is (from the "Compound Formats" list in the mentioned documentation) for example:
10/Oct/2000:13:55:36 -0700,
2008:08:07 18:11:31,
2008-08-07 18:11:31,
2008-07-01T22:35:17.02,
2008-07-01T22:35:17.03+08:00,
20080701T22:38:07,
20080701T9:38:07,
20080701t223807,
20080701T093807,
2008-7-1T9:3:37,
(this is really copy of the documentation)
Use it like this strtotime("YYYY-mm-DD HH:MM AM/PM"):
echo date("d F Y h:i:s A", strtotime("2011-06-01 11:15 PM")) . "\n";
OUTPUT
01 June 2011 11:15:00 PM
Y-m-d hh:mm will work
echo strtotime('2011-12-14 11:44 am');
cit #Pekka :)
strtotime($month."-".$day."-".$year)
can anyone get me a code for hiding the time stamp from a string.
I used this code to get the date from the string
suppose the
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date = strtotime($date_string);
$date = date('m/d/y', $date);
But the out put I am getting is something like this
1/1/70
Please suggest a better way that I could implement for this to work
I want it to show like
02/06/2011
If the date you're looking for is already in the string and all you want to do is remove the time range, you don't need any date manipulation. Just remove everything after the space (assuming the format of the date_string remains consistent).
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date = explode(" ",$date_string);
echo $date[0];
Or even simpler (but untested)
echo strtok($date_string," "); //http://codepad.org/Or1mpYOp
PHP.NET:strtok
PHP.NET:explode
$date = strtotime($date_string);
$date = getdate($date);
$date = $date['mon'] . '/' . $date['mday'] . '/' . $date['year']
If 02/06/2011 11:00 am - 2:00 pm is what gets displayes, you're obviously displaying $date_string and not $date, because strtotime('02/06/2011 11:00 am - 2:00 pm'); returns boolean false, which date('m/d/y', $date) would convert to 01/01/1970.
Try something like this
$date_string = "02/06/2011 11:00 am - 2:00 pm";
$date_exploded = explode('-',$date_string);
$date = strtotime($date_exploded[0]);
$date = date('m/d/y', $date);
echo $date;
Leaving aside the fact that something is going very wrong in the date parsing, you need to be aware that using a date format of 00/00/00[00] is rather ambigious - in the US dates written like this are in the format mm/dd/yy[yy] while in the UK it is interpreted as dd/mm/yy[yy]. The strtotime function does not use the locale setting to work out which applies and always assumes the former.
If I were being asked to parse this, I'd go with using preg to extract the date part. Using a pattern:
/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/
gives an array
0=> 02/06/2011
1=> 02
2=> 06
3=> 2011
Then use mktime to generate the unix timstamp.
Other approaches include using substr to extract a fixed length string, or exploide by space to get words.
If your string is already is date .
$date = substr($records[$datetime, 0, 10);
How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));