This question already has answers here:
PHP convert short month-name to month-number [duplicate]
(7 answers)
Closed 7 years ago.
$month_name = 'Feb';
$month_number = date("m", strtotime($month_name));
From above code, I get the output is 03 not 02. Why?
I suppose it's caused by current date and default value of date in strtotime function. Today we have 31 December so if you use strtotime, default value of date will be 31 Dec 2015, but if you change month to Feb the date will be 03 Mar 2015. Solution here is to add first day number at the begining e.g.
$month_name = '1 Feb';
$month_number = date("m", strtotime($month_name));
You can use the function date_parse():
$month_name = 'Feb';
$date = date_parse($month_name);
echo $date['month'];
what about like this ... simple way ...
$mons = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
$month_name = 'Feb';
$month_number = $mons[$month_name];
You should pass a full date as follows:
$month_name = 'Feb';
$month_number = date("m", strtotime("1-".$month_name."-2015"));
strtotime will not work like that. You can check in PHP Manual. Here you can use date_parse.
$month = 'Feb';
$month = date_parse($month);
echo $month['month'];
This function will return you an array like:
Array
(
[year] =>
[month] => 2
[day] =>
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 1
[warnings] => Array
(
[4] => The parsed date was invalid
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
Now, you can get the month name or anything else.
Or, you still want to use strtotime function than you can try it as:
$month = '1 Feb';
$month = date("m",strtotime($month));
echo $month;
Related
I have a start_date of 1/10/2018, and an end_date of 1/8/2020, the difference between the two dates in months is 22, that is 1 year 10 months, now, I want to create tables that terminate at the end of each year as follows:
table 1
column_heading will be "1/10/2018 - 31/12/2018"
and the row will be "2 months"
table 2
column_heading will be "1/1/2019 - 31/12/2019"
and the row will be "12 months"
table 3
column_heading will be "1/1/2020 - 1/8/2020"
and the row will be "8 months"
I would like to loop something, maybe the difference between the dates to create the number of tables necessary, if the two dates exist within the same year it will only create 1 table, or 2 if it enters the next year, I am using laravel and carbon to manipulate the dates.
Thank you in anticipation of your help.
Something like this
Here's one way. Note that I had to convert the format of your dates to YYYY-mm-dd in order to use PHP date functions. In the end you'll get an array and it's easy for you to transform the final dates into the format you desire. You can test it here: https://www.tehplayground.com/lvuTdWl91TeItEQC
The code:
<?php
// example code
$s = "1/10/2018";
$e = "1/08/2020";
// reassemble so we can use the date functions YYYY-mm-dd
$s = implode("-", array_reverse(explode("/", $s)) );
$e = implode("-", array_reverse(explode("/", $e)) );
// get the parts separated
$start = explode("-",$s);
$end = explode("-",$e) ;
$iterations = ((intVal($end[0]) - intVal($start[0])) * 12) - (intVal($start[1]) - intVal($end[1])) ;
$sets=[$start[0] => array("start" => $s, "end" => "", "months" => 0)];
$curdstart= $curd = $s;
$curyear = date("Y", strtotime($s));
for($x=1; $x<=$iterations; $x++) {
$curdend = date("Y-m-d", strtotime($curd . " +{$x} months"));
$curyear = date("Y", strtotime($curdend));
if (!isset($sets[$curyear])) {
$sets[$curyear]= array("start" => $curdend, "end" => "", "months" => 0);
}
$sets[$curyear]['months']++;
$sets[$curyear]['end'] = date("Y-m-", strtotime($curdend)) . "31";
}
die(print_r($sets,1));
$mctr = 0 ;
The output:
Array
(
[2018] => Array
(
[start] => 2018-10-1
[end] => 2018-12-31
[months] => 2
)
[2019] => Array
(
[start] => 2019-01-01
[end] => 2019-12-31
[months] => 12
)
[2020] => Array
(
[start] => 2020-01-01
[end] => 2020-08-31
[months] => 8
)
)
This question already has answers here:
Display all the week numbers between two dates in PHP [duplicate]
(3 answers)
Closed 8 years ago.
Given a start and end date, I need to generate an array with year and week values.
For example:
Start Date: 2013-01-01
End Date: 2013-02-23
Generated Array:
Array ( [0] => Array ( [week] => 01 [year] => 2013 )
[1] => Array ( [week] => 02 [year] => 2013 )
[2] => Array ( [week] => 03 [year] => 2013 )
[3] => Array ( [week] => 04 [year] => 2013 )
[4] => Array ( [week] => 05 [year] => 2013 )
[5] => Array ( [week] => 06 [year] => 2013 )
[6] => Array ( [week] => 07 [year] => 2013 )
[7] => Array ( [week] => 08 [year] => 2013 ) )
Here is the code I'm using to generate this:
public static function getYearWeekRange($startdate, $enddate) {
$array = array();
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
while ($starttime <= $endtime) {
$year = date('Y', $starttime);
$week = date('W', $starttime);
$array[] = array('week' => $week, 'year' => $year);
$starttime = strtotime('+1 week', $starttime);
}
return $array;
}
My problem is that when I generate certain date ranges, I don't get the correct year value at the start of the 2013 year. For example:
Start Date: 2012-01-01
End Date: 2013-02-23
In this case, where it should have an subarray with year = 2013 and week = 01, it actually has it's year value equal to 2012.
If I were to switch the start date to 2013-01-05 for example, then there is no problem.
Can anyone offer a solution that would guarantee that my year and week values are always correct?
I was able to fix my problem using the following:
public static function getWeekRange($startdate, $enddate) {
$array = array();
$p = new DatePeriod(
new DateTime($startdate),
new DateInterval('P1W'),
new DateTime($enddate)
);
foreach ($p as $w) {
$array[] = array('year' => $w->format('o'), 'week' => $w->format('W'));
}
return $array;
}
I want to calculate the difference between today and next year April 4 (right now is 4/4/2013), but I don't know how to create a DateTime object using actual year + 1. This is what I have:
$now = new DateTime();
$ref = mktime(0, 0, 0, 4, 3, date("Y")+1);
echo $diff = $ref->diff($now)->days;
I think the problem is that mktime is not returning a DateTime object? Which is the best way to do this?
Thanks
$today = date("Y-m-d");
$destination = date("Y-m-d" , mktime(0, 0, 0, 4, 3, date("Y")+1));
$todayObj = new DateTime($today);
$destObj = new DateTime($destination);
echo $diff = $todayObj->diff($destObj)->days;
Didn't test it yet.
Use strtotime for this as below
$nextyear = date('Y')+1;
$time1=$nextyear.'-'.date('m').'-'.date('d');
$time2=date('Y-m-d');
echo $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Try this:
print_r(date_diff(date_create(), date_create('2013-04-03 00:00:00')));
Outputs:
DateInterval Object
(
[y] => 0
[m] => 5
[d] => 22
[h] => 9
[i] => 8
[s] => 25
[invert] => 0
[days] => 173
)
I'm passing a date value in the following format.
2011-09-10
I need to break it into 3 variables using php.
$day =
$month =
$year =
How should I go about doing this?
For your case, $parts = explode("-", $inputstr); would work (and then year is $parts[0] et cetera).
But, for more general date parsing, you might want strptime() (if you know the format) or strtotime() (if you don't).
In one line:
list($year, $month, $day) = explode('-', '2011-09-10');
$date = getdate(strtotime("2011-09-10"));
print_r($date);
Output:
Array
(
[seconds] => 0
[minutes] => 0
[hours] => 0
[mday] => 10
[wday] => 6
[mon] => 9
[year] => 2011
[yday] => 252
[weekday] => Saturday
[month] => September
[0] => 1315612800
)
See PHP's explode() function
I have a date in the following format
November 18, 2009, 3:00PM
How can i break that up so that i can store each value as its own variable?
such as...
$month //November
$day //18
$year //2009
$hour //03
$minute //00
$ampm //PM
Use the 'date_parse' (http://nl2.php.net/manual/en/function.date-parse.php) function. It returns an array with the parsed items:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
Convert your date into a timestamp, then with the timestamp you can easily get your parts. An other way is using a regular expression.
$str = "November 18, 2009, 3:00PM";
list($month,$day,$year,$time) = preg_split('/[ ,]/',$str,false,PREG_SPLIT_NO_EMPTY);
preg_match('/([0-9]+):([0-9]+)([AP]M)/',$time,$timeparts);
list($time,$hour,$minute,$ampm) = $timeparts;
echo "\$month $month\n";
echo "\$day $day\n";
echo "\$year $year\n";
echo "\$hour $hour\n";
echo "\$minute $minute\n";
echo "\$ampm $ampm\n";
Output
$month November
$day 18
$year 2009
$hour 3
$minute 00
$ampm PM
More complex solution.
If your dates may be in the different standards you can use date() function (http://php.net/manual/en/function.date.php) + strtotime() function (http://php.net/manual/en/function.strtotime.php), which parse string and returns the unix timestamp.
For example, if you want to get a year from your date string you could write next code:
$date = 'November 18, 2009, 3:00PM';
$year = date('Y', strtotime($date));
Or, if you want to know how much days in the month in date you get, you could write such code:
$date = 'November 18, 2009, 3:00PM';
$num_of_days = date('t', strtotime($date));
't' returns the number of days in the given month.