How to count including the zero like substr in php. I'm doing like this query.
$datavariable = $query->result();
$caldata = array();
foreach ($datavariable as $row) {
$caldata[substr($row->start_date, 8, 2)] = $row->class;
}
If the date is 2019-05-06 it didn't get the 01-09 only the 10-up can you help me or is their a function like that? I just want to get the days
Don't use substr() to get the day from the date. Use date() and strtotime() for this
$day = date('d', strtotime($row->start_date));
$caldata[$day] = $row->class;
Read more about date() and strtotime()
Related
I need to compare two dates and find which date is greater.
$actual_date = Carbon::createFromFormat('d-m-Y',$night_out->actual_return_date);
$expected_date = Carbon::createFromFormat('d-m-Y', $night_out->expected_return_date);
$days = $expected_date->diffInDays($actual_date); // gives the days count only
Thanks in Advance!
You can use carbon method greaterThan()
if($actual_date->greaterThan($expected_date)){
// logic here
}
Carbon is an extension of datetime and inherits all properties of the base class. DateTime objects and thus carbon objects are directly comparable. Special comparison methods are not needed for this case.
if($actual_date > $expected_date){
// do something
}
If only the date which is greater is needed, you can do that
$max_date = max($actual_date , $expected_date);
Note: $ max_date is an object reference of $actual_date or $expected_date. You can get a copy with the copy () method or use clone.
$max_date = max($actual_date , $expected_date)->copy();
Use gt function for that:
$actual_date = Carbon::createFromFormat('d-m-Y',$night_out->actual_return_date);
$expected_date = Carbon::createFromFormat('d-m-Y', $night_out->expected_return_date);
if($expected_date->gt($actual_date)){
return $expected_date; //Max date
} else {
return $actual_date; //Min date
}
OR:
You need to find a greater date from two dates using max and array_map function like:
$actual_date = Carbon::createFromFormat('d-m-Y',$night_out->actual_return_date);
$expected_date = Carbon::createFromFormat('d-m-Y', $night_out->expected_return_date);
// store dates value into an array to find the max date.
$date = array();
$date['actual_date'] = $actual_date;
$date['expected_date'] = $expected_date;
$max = max(array_map('strtotime', $date));
echo date('d-m-Y', $max);
So I have an array of objects that looks like this:
credit_cards[[0] => {expiration_date: {year: "17", month: "04"}]
What I try to do is somethings like this:
$years = [];
$months = [];
$dates = [];
foreach ($clients->settings->credit_cards as $key => $value){
$years[] = $value->expiration_date->year;
$months[] = $value->expiration_date->month;
}
I need to loop through every credit card in that array and make an array $dates that should form a basic date by concatenating years and months. So in this case it should look like this: $dates = ['2017-04']. But I have no idea how to do this. Any help is welcomed and some explanation if you may so I can understand how to do this in the future.
You can just add the dates with a minor amount of work. create a parseable string that represents your Month / Year. Then put it in php's strtotime(). That integer representing seconds since the epoch is the 2nd argument to format the four digit date -dash- month that you are looking for.
$years[] = $value->expiration_date->year;
$months[] = $value->expiration_date->month);
$my = $value->expiration_date->month.'/'.$value->expiration_date->year;
$dates[] = date('Y-m', strtotime($my));
You're looking for the concatenate operator: . (More info here.) You can build a formatted date string and pass it to strtotime like WEBjuju's answer, or move directly to using the concatenation.
$years[] = $value->expiration_date->year;
$months[] = $value->expiration_date->month;
$dates[] = $value->expiration_date->year . '-' . $value->expiration_date->month;
This question already has answers here:
Parse and reformat a datetime string
(6 answers)
Closed 11 months ago.
I have a string with a date which is in this format MMDDYYYY (ie. 01132012, 01142012 etc.)
I need to do something on a page, if that string is 14 days or less from the current date.
ie. Today is 01132012, so any strings with 12312011 or a less date are going to be showing something on a page.
Can anyone help with this? I've tried
echo date("d/m/Y", strtotime('01142012'));
But to no avail.
You can use the DateTime class of PHP
<?
// current date
$now = new DateTime();
//your date
$date = DateTime::createFromFormat('mdY', '01142012');
// calculate difference
$diff = $now->diff($date);
...
// output the date in format you want
echo $date->format('d/m/Y');
?>
EDIT: I just realized, that your format isn't one supported by php. So you have to use alternate objectbuild.
I prefer using strptime.
<?
$dt = strptime('01142012', '%m%d%Y');
echo sprintf("%02d/%02d/%04d", $dt['tm_mday'], $dt['tm_mon']+1, $dt['tm_year']+1900);
If you use PHP 5.3 or above, you can also use date_parse_from_format()
How about some substr + mktime?
$string = '01142012';
$time = mktime(0, 0, 0,
substr($string, 0, 2),
substr($string, 2, 2),
substr($string, 4, 4)
);
echo date('d/m/Y', $time);
try date('m-d-y', strtotime('01142012'));
could also try something like;
$var = strtotime('01142012');
$var2 = date ('F j, Y', $var);
Your string input of '01142012' cannot be parsed by strtotime() as it is not a valid as it is returning -1 as an answer. To convert this into a valid date you will need to add either slashes or dashes to separate the numbers.
The easiest way would be to store the dates with the dashes or slashes, such as '01-14-2012' or '01/14/2012' in the database from now on or you are going to have to create your own function to convert the numbers into a valid form for strtotime().
To do this you could do something like this:
function makeValidDate($date) {
$valid_date = array();
$array = str_split($date); //split characters up
foreach($array as $key => $character){
if($key==2 || $key==4){
$character = '-'.$character; //add relevant formatting to date
$valid_date[] = $character; //add this to the formatted array
}
else{
$valid_date[] = $character; // if not dashes or slashes needed add to valid array
}
}
return implode($valid_date); // return the formmatted date for use with strtotime
}
You can then do this to get a valid date:
$valid_date = makeValidDate('01142012');
echo date("d/m/Y", strtotime($valid_date));
I haven't tested this but you should get a good idea of what to do.
EDIT: Capi's idea is a lot cleaner!!
try "preg_match(pattern,string on wich the pattern will be aplied)";
http://www.php.net/manual/en/function.preg-match.php
you can also define an offset. so first take te first 2 digits. than take the other 2 digits and after that get the other four digits. after that place them in one string. after that use maketime,strtotime,date. this kind of stupid solution but i only thought of that. hope this will help
I have a problem getting certain data from my database by querying a date range. In my database i have a DATE type field within the format YYYY-MM-DD. I want to get all data within a date range of today + 2 weeks (Expiring).
I have wrote:
$format = 'Y-m-j';
$date = date ( $format );
$new = date ( $format, strtotime ( '+14 day' . $date ) );
$start = date("Y-m-d", strtotime($new));
$today = date('Y-m-d');
$q = "SELECT * FROM listing WHERE dd_end BETWEEN '".$today."' AND '".$start."'";
while($row = mysql_fetch_assoc($q)){
$listing_id = $row['listing_id'];
echo "$listing_id";
}
So what I want to achieve is for the query to pull all the rows from now until 5th October. I've echo'd the variables and they show they're in the correct form (YYYY-MM-DD) to compare within the database but no results are returning.
Any help would be greatly appreciated. Many thanks in return.
Well, assuming that the mysql database has the same date that your server, you could let the mysql database do all the date calculations.
A little something like this:
SELECT *
FROM listing
WHERE dd_end BETWEEN CURDATE() AND (CURDATE() + INTERVAL 14 DAY)
On the other hand, i think Paul S's answer may fix your problem.
Edit:
You forgot to call mysql_query before the mysql_fetch_assoc() function.
$result = mysql_query($q);
while ($row = mysql_fetch_assoc($result))
{
$listing_id = $row['listing_id'];
echo "$listing_id";
}
If dd_end is a date you may want to read a certain section on the MySQL docs: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
For best results when using BETWEEN with date or time values, use
CAST() to explicitly convert the values to the desired data type.
Examples: If you compare a DATETIME to two DATE values, convert the
DATE values to DATETIME values. If you use a string constant such as
'2001-1-1' in a comparison to a DATE, cast the string to a DATE.
May this is the right way ?
$start = date("Y-m-d", strtotime('+14 day' . $date));
Read:
http://php.net/manual/en/function.strtotime.php
strtotime has a second argument.
$format = 'Y-m-j';
$date = date ( $format );
$new = date ( $format, strtotime ( '+14 day' . $date ) );
$start = date("Y-m-d", strtotime($new));
Should be:
$new = strtotime('+14 day', time());
$start = date("Y-m-d", $new);
$today = date('Y-m-d');
$q = mysql_query("SELECT * FROM listing WHERE dd_end BETWEEN '".$today."' AND '".$start."'");
while($row = mysql_fetch_assoc($q)){
$listing_id = $row['listing_id'];
echo "$listing_id";
}
I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');