Bug with strtotime PHP - php

echo date("m", strtotime("january"));
Returns 01 as expected
echo date("m", strtotime("february"));
But this returns 03
Anyone else encountered this problem?
PHP Version 5.1.6

Today is the 29th. There is no 29th in February this year and because you're not specifying a day in February, it's using "today". The strtotime function uses relative dates so the 29th of February is basically the 1st March this year.
To solve your problem:
echo date("m", strtotime("February 1"));

As strtotime() only handles English date formats, you should maybe try using this function that I just made for you. With this you can handle month names in other languages too.
Don't know if this is essential to your application, but now you have it.
function getMonth($month, $leadingZero = true) {
$month = strtolower(trim($month)); // Normalize
$months = array('january' => '1',
'february' => '2',
'march' => '3',
'april' => '4',
'may' => '5',
'june' => '6',
'july' => '7',
'august' => '8',
'september' => '9',
'october' => '10',
'november' => '11',
'december' => '12',
'dezember' => '12', // German abrevation
'marts' => '3', // Danish abrevation for March
);
if(isset($months[$month])) {
return $leadingZero ? substr('0' . $months[$month], -2) : $months[$month];
} else {
return false;
}
}

Related

Get Start and end date of Week according to string

I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )

PHP convert multiple array-values to time format [duplicate]

This question already has answers here:
Create a new date from php array
(3 answers)
Closed 4 years ago.
For a project, i need to convert multiple values form an array to a time format in PHP. I've got arrays like this:
starttime = ['year' => 2019, 'month' => 5, 'day' => 10, 'hour' => 20, 'minute' => 15]
Is there a way to convert these values to a time format with which i can also calculate stuff? I already tried using strtotime, but it dind't work. Thanks for your answers!
I suggest using the DateTime object which has the setDate, setTime and getTimestamp methods. Which you can use for defining the date and time from the array keys, and retrieve the unix timestamp as a result.
Example: https://3v4l.org/o3lVr
$starttime = ['year' => 2019, 'month' => 5, 'day' => 10, 'hour' => 20, 'minute' => 15];
$date = new \DateTime();
$date->setDate($starttime['year'], $starttime['month'], $starttime['day']);
$date->setTime($starttime['hour'], $starttime['minute']);
var_dump($timestamp = $date->getTimestamp());
var_dump(date('Y-m-d H:i:s', $timestamp));
Results:
int(1557512100)
string(19) "2019-05-10 20:15:00"
Optionally to prevent issues with missing keys, in PHP >= 7.0 you can use the null coalesce operator ?? to default the values to the current date.
Example: https://3v4l.org/0MOI5
$starttime = ['month' => 5, 'day' => 10, 'minute' => 15];
$date = new \DateTime();
$date->setDate($starttime['year'] ?? $date->format('Y'), $starttime['month'] ?? $date->format('m'), $starttime['day'] ?? $date->format('d'));
$date->setTime($starttime['hour'] ?? $date->format('H'), $starttime['minute'] ?? $date->format('i'));
var_dump($timestamp = $date->getTimestamp());
var_dump(date('Y-m-d H:i:s', $timestamp));
Results:
int(1557497700)
string(19) "2019-05-10 16:15:00"
Alternatively you can also use mktime to produce the same result. Please note, as per the manual, in PHP < 5.1.0 this method may produce unexpected (but not incorrect) results if DST is not specified.
Example: https://3v4l.org/DU0Q1
$starttime = ['year' => 2019, 'month' => 5, 'day' => 10, 'hour' => 20, 'minute' => 15];
$timestamp = mktime(
$starttime['hour'],
$starttime['minute'],
0,
$starttime['month'],
$starttime['day'],
$starttime['year']
);
var_dump($timestamp);
var_dump(date('Y-m-d H:i:s', $timestamp));
Results:
int(1557512100)
string(19) "2019-05-10 20:15:00"

Converting Mysql text to date

I currently am working with a database that has a month column and a year column.. Both are of type text. Month is stored as 'January' and year is stored as you would expect, '2016'..
Any recommendations for concatenating these and converting them to a date type?
You can use a query like this. you only must change the strings to your fieldnames:
sample
select STR_TO_DATE(concat('2016',' ','April','1'), '%Y %M %D');
result
2016-04-01
$months = [
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' =>10,
'november' =>11,
'december' =>12
];
$year = 2016;
$month_text = 'january';
$day = 1;
if($months[strtolower($month_text)]<10){
$month = '0'.$months[strtolower($month_text)];
}else{
$month = $months[strtolower($month_text)];
}
if($day<10){
$day = '0'.$day;
}else{
$day = $day;
}
echo $year.'-'.$month.'-'.$day;
create a new feld in db with type date and insert it into the db. maybe wrap this in a function and put it in a while loop..?
This works for me:
select convert(date,(concat('January',' ','2016')))

Query Logic For mysql and php

we have this company where they work on a yearly period of april"current year" to march"next year"
Im writing and excel report to pull out reports based on that period. where Jan, Feb, and march would be the next year , but I keep on getting the current year do you get me?
bus_year = is the year inform Jan to Dec, but its actually April to march
and year is year
I would like to know how, I could write a good logic whereby on selected months the logic would plus 1 year to the current, from instead of Jan 2013 to Jan 2014. I'm trying to brige years , and could not use unix_timestamp solely because Its just been implemented noe in the database and the is more than 5 year of data in it.
current code
private $month_array = array(
'01' => "January",
'02' => "February",
'03' => "March",
'04' => "April",
'05' => "May",
'06' => "June",
'07' => "July",
'08' => "August",
'09' => "September",
'10' => "October",
'11' => "November",
'12' => "December"
);
private $financial_year_month = array(
'04' => '01',
'05' => '02',
'06' => '03',
'07' => '04',
'08' => '05',
'09' => '06',
'10' => '07',
'11' => '08',
'12' => '09',
'01' => '10',
'02' => '11',
'03' => '12'
);
maybe to right in in a form like this for example
04-$year, 05-year, ... , 02-($year+1), 03-($year+1)
//sql query
public function get($year)
{
$select = $this->select();
$select->where('bus_year = ?', $year);
return $this->fetchAll($select);
}
Database columns Id/month/year/bus_year
Not sure how I can define and initialize year at the moment. Something like this but my current code wont take it, don't know what Im missing
if($month >= 01 && $month <= 03){ $year = $year + 1;

MYSQL search query in Joomla

I am currently editing a (very messy!) custom AJAX filter component in order for a user to be able to search for times of events by time of day (Morning, Afternoon, Evening). The system currently searches records by hour of day (12, 08, etc) and uses the hour submitted in the filtering to perform a LIKE search on the database to return results.
I have grouped hours into time-of-day arrays as follows in the model for the filtering:
if($time == 'morning') {
$times = array('07', '08', '09', '10', '11');
}
if($time == 'afternoon') {
$times = array('12', '13', '14', '15', '16');
}
if($time == 'evening') {
$times = array('17', '18', '19', '20', '21');
}
The current query being used is as follows:
if ($time>0){
$query->where('a.startTime LIKE "%'.$time.'%"');
}
I need to change the query for it to be able to seach the database to check whether the value in the database 'time' field is in my $times array and also perform a like as it's only the first two characters of the string I need to be matching. This is because values in the database could be varying (15.30, 15.45, etc) but only need to match the hour (15).
I have tried looping through the items in my array creating a 'like' and 'or' query but with no success. Does anybody have any ideas?
Many Thanks
$per=array('morning'=>array(7, 8, 9, 10, 11),
'afternoon'=>array(12, 13, 14, 15, 16),
'evening'=>array(17, 18, 19, 20, 21));
$query->where('HOUR(a.startTime) IN ('.implode(',',$per[$need].')');
or
$per=array('morning'=>array('min'=>7, 'max'=>11),
'afternoon'=>array('min'=>12,'max'=> 16),
'evening'=>array('min'=>17,'max'=> 21));
$query->where('HOUR(a.startTime) >= '.$per[$need]['min'].' and HOUR(a.startTime)<='.$per[$need]['max']);
Maybe something like this
$time=15.30;//for example
if ($time>0){
$time=preg_replace('~\.(.*?$)~','',$time);//time is now 15
$morning = array('07', '08', '09', '10', '11');
$afternoon = array('12', '13', '14', '15', '16');
$evening = array('17', '18', '19', '20', '21');
if(in_array($time,$morning)){
echo 'morning';
}else if(in_array($time,$afternoon)){
echo 'afternoon';
}else if(in_array($time,$evening)){
echo 'evening';
}
$query->where('a.startTime LIKE "%'.$time.'%"');
}

Categories