Here's my code:
$date_depo = date('d-m-Y', strtotime($date_depot));
So in my excel table I have => 2 nov 16 00:00:00
and this code changes it to => 02-11-2016
But if I have 10 mai 13 00:00:00 it changes it to 01-01-1970
it means that the value is 0 or something like that, how can I change it to => 10-05-2013
Edit:
setlocale (LC_TIME, 'fr_FR.utf8','fra');
$date_depo = strftime('%Y-%m-%d', $date_depot);
Doesn't work.
$trans = array('janv' => 'January ','févr' => 'February','mars' => 'March ','avr' => 'April','mai' => 'May','juin' => 'June','juil' => 'July','août' => 'August','sept' => 'September','oct' => 'October','nov' => 'November','déc' => 'December');
$resulat= strtr($date_depot,$trans);
$date_depot_t = date('d-m-Y', strtotime($resulat));
Had to replace every month, but it works!
Related
help me please
I have a code
<?php
// date on Russian
function getDateRus(){
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
return ( (int)date('d') . ' ' . $monthes[(date('n'))] . date(' Y'));
}
// day on Russian
function getDayRus(){
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
return $days[(date('w'))];
}
echo "Today:" . getDateRus() . ", " . getDayRus();
?>
I need to put days off for a week ahead:
25 September, Monday
26 September, Tuesday
27 September, Wednesday
28 September, Thursday
29 September, Friday
30 September, Saturday
01 October, Sunday
I can print the dates for the week ahead, but without the Russian language.
how to deduce the dates for the week ahead and save the Russian language?
I changed your structure slightly and I made use of the powerful function DateTime
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
$dArray = array();
$date = new DateTime();
for($i=0; $i<7;$i++){
$dArray[] = $date->format("d").' '.$monthes[$date->format("n")].','.$days[$date->format("w")];
$date->modify("+1 day");
}
print_r($dArray);
Output:
Array ( [0] => 25 Сентября,Понедельник [1] => 26 Сентября,Вторник [2] => 27 Сентября,Среда [3] => 28 Сентября,Четверг [4] => 29 Сентября,Пятница [5] => 30 Сентября,Суббота [6] => 01 Октября,Воскресенье )
You can probs achieve this better by using strftime and setlocale. That way you will not need your day/month arrays
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;
I have a problem with strtotime and the following code (I want all calendar weaks in the given time range)
$all_dates = array();
$tempdate = '2014-01-01';
$enddate = '2015-03-01';
while (strtotime($tempdate) <= strtotime($enddate)) {
$all_dates[] = date('Y-W', strtotime($tempdate));
$tempdate = date('Y-m-d', strtotime($tempdate . '+1 week'));
}
var_dump($all_dates);
the var_dump produces the following output
array (
0 => '2014-01',
1 => '2014-02',
2 => '2014-03',
3 => '2014-04',
4 => '2014-05',
5 => '2014-06',
6 => '2014-07',
7 => '2014-08',
8 => '2014-09',
9 => '2014-10',
10 => '2014-11',
11 => '2014-12',
12 => '2014-13',
13 => '2014-14',
14 => '2014-15',
15 => '2014-16',
16 => '2014-17',
17 => '2014-18',
18 => '2014-19',
19 => '2014-20',
20 => '2014-21',
21 => '2014-22',
22 => '2014-23',
23 => '2014-24',
24 => '2014-25',
25 => '2014-26',
26 => '2014-27',
27 => '2014-28',
28 => '2014-29',
29 => '2014-30',
30 => '2014-31',
31 => '2014-32',
32 => '2014-33',
33 => '2014-34',
34 => '2014-35',
35 => '2014-36',
36 => '2014-37',
37 => '2014-38',
38 => '2014-39',
39 => '2014-40',
40 => '2014-41',
41 => '2014-42',
42 => '2014-43',
43 => '2014-44',
44 => '2014-45',
45 => '2014-46',
46 => '2014-47',
47 => '2014-48',
48 => '2014-49',
49 => '2014-50',
50 => '2014-51',
51 => '2014-52',
52 => '2014-01',
53 => '2015-02',
54 => '2015-03',
55 => '2015-04',
56 => '2015-05',
57 => '2015-06',
58 => '2015-07',
59 => '2015-08',
60 => '2015-09',
)
The Problem is entry number 52 with '2014-01', it should be '2015-01'. With the timerange 2015-01-01 till 2016-03-01 the result is ok. Is this a bug in strtotime function?
I'm using PHP version 5.5.10
Thanks for your help.
Change this line and use the o iso year:
$all_dates[] = date('o-W', strtotime($tempdate));
// ^
This has to do with ISO week numbers. Also, strtotime() is a bad way to do date math and PHP recommends against it (thanks to leap years and daylight savings). Use the DateTime() classes to do this with accuracy:
$startdate = new DateTime('2014-01-01');
$enddate = new DateTime('2015-03-01');
$interval = new DateInterval('P7D');
$period = new DatePeriod($startdate , $interval, $enddate);
foreach($period as $date) {
$all_dates[] = $date->format('o-W'); // o modifier for ISO year
}
var_dump($all_dates);
Demo
Assume that selected date from Canlender is 02/09/2011. To store weekly date into array from 20/09/2011 is
for($i=0; $i<7; $i++)
{
$WeeklyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
My question is how to store monthly date into array from the selected date.
Many thanks
---Update----------
The final result of monthlyDate should look like the following:
$monthlyDate= array{2011-08-03, 2011-08-04, 2011-08-05, 2011-08-06, 2011-08-07 ....2011-08-31, 2011-09-01, 2011-09-02}
First, calculate the number of days in a month using cal_days_in_month and then proceed as you are doing with weeks eg:
$days = cal_days_in_month(CAL_GREGORIAN, 9, 2011);
for($i = 0; $i <= $days; $i++)
{
$MonthlyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
Notice that CAL_GREGORIAN is a built-in constant.
Working Example
Whenever programs are incrementing a date using 86400 there is a risk of unexpected output because of DST.
By using strtotime() with a unit larger than hours (like days, weeks, months, etc.) preventing any DST hiccups. Note: a DateTime object approach can be used but for this case, it is unnecessary overhead.
The following is an adjusted form of a one-liner date range function I developed.
Here is the online demo for this case.
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
$date='2011-09-02';
$monthlyDate=getDatesFromRange(date("Y-m-d",strtotime("$date -1 month +1 day")),$date);
var_export($monthlyDate);
output as desired/expected:
array (
0 => '2011-08-03',
1 => '2011-08-04',
2 => '2011-08-05',
3 => '2011-08-06',
4 => '2011-08-07',
5 => '2011-08-08',
6 => '2011-08-09',
7 => '2011-08-10',
8 => '2011-08-11',
9 => '2011-08-12',
10 => '2011-08-13',
11 => '2011-08-14',
12 => '2011-08-15',
13 => '2011-08-16',
14 => '2011-08-17',
15 => '2011-08-18',
16 => '2011-08-19',
17 => '2011-08-20',
18 => '2011-08-21',
19 => '2011-08-22',
20 => '2011-08-23',
21 => '2011-08-24',
22 => '2011-08-25',
23 => '2011-08-26',
24 => '2011-08-27',
25 => '2011-08-28',
26 => '2011-08-29',
27 => '2011-08-30',
28 => '2011-08-31',
29 => '2011-09-01',
30 => '2011-09-02',
)
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