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
Related
I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo
I receive a datetime from a plugin. I put it into a variable:
$datetime = "20130409163705";
That actually translates to yyyymmddHHmmss.
I would need to display this to the user as a transaction time but it doesn't look proper.
I would like to arrange it to be like 09/04/2013 16:37:05 or 09-apr-2013 16:37:05.
How do I go about and change the orders of the string?
As for now I could think is to use substr to separate the date and time. I'm still not sure on how to add the additional characters and rearrange the date.
why not use date() just like below,try this
$t = strtotime('20130409163705');
echo date('d/m/y H:i:s',$t);
and will be output
09/04/13 16:37:05
For PHP 5 >= 5.3.0 http://www.php.net/manual/en/datetime.createfromformat.php
$datetime = "20130409163705";
$d = DateTime::createFromFormat("YmdHis", $datetime);
echo $d->format("d/m/Y H:i:s"); // or any you want
Result:
09/04/2013 16:37:05
date("Y-m-d H:i:s", strtotime("2019-05-13"))
If you want to use substr(), you can easily add the dashes or slashes like this..
$datetime = "20130409163705";
$yyyy = substr($datetime,0,4);
$mm = substr($datetime,4,6);
$dd = substr($datetime,6,8);
$hh = substr($datetime,8,10);
$MM = substr($datetime,10,12);
$ss = substr($datetime,12,14);
$dt_formatted = $mm."/".$dd."/".$yyyy." ".$hh.":".$MM.":".$ss;
You can figure out any further formatting from that point.
You have different options.
Using date():
$format = date('d/m/y H:i:s', 1621371929);
echo $format;
the output is:
18/05/21 14:05:29
Using date_format():
$date = date_create(1621371929);
echo date_format($date, 'd/m/y H:i:s');
the output is:
18/05/21 14:05:29
Using DateTime::format():
$date = new DateTime('2021-05-18 14:05:29');
echo $date->format('d/m/y H:i:s');
the output is:
18/05/21 14:05:29
This article gives you an overview of these three methods, and this one is DateTime::format() in depth.
try this
$datetime = "20130409163705";
print_r(date_parse_from_format("Y-m-d H-i-s", $datetime));
the output:
[year] => 2013
[month] => 4
[day] => 9
[hour] => 16
[minute] => 37
[second] => 5
You could do it like this:
<?php
$datetime = "20130409163705";
$format = "YmdHis";
$date = date_parse_from_format ($format, $datetime);
print_r ($date);
?>
You can look at date_parse_from_format() and the accepted format values.
https://en.functions-online.com/date.html?command={"format":"l jS \\of F Y h:i:s A"}
You can use date_parse_from_format() function ...
date_parse_from_format(string $format, string $datetime): array
Example
<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>
The above example will output:
Array
(
[year] => 2009
[month] => 1
[day] => 6
[hour] => 13
[minute] => 0
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] => 1
[zone_type] => 1
[zone] => 3600
[is_dst] =>
)
Check the PHP docs..you will get clear idea
I have some date, like:
20 November 06:10
12 November 08:12
10 October 13:23
There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?
Try this:
$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");
foreach($dates as $d){
$exploded = explode(" ", $d);
$newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);
//print_r($newDate);
$time = strtotime(implode($newDate));
echo $time."<br/>";
}
The output i got is:
1349868180
1352704320
1349868180
The logic is:
You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.
This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.
I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.
You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.
print_r(date_parse_from_format("d F H:i", '20 November 06:10'));
gives you:
Array
(
[year] =>
[month] => 11
[day] => 20
[hour] => 6
[minute] => 10
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
How do you get today's date, as a date object?
I'm trying to compute the difference between some start date and today. The following will not work, because getdate() returns an array and not a date object:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
Output:
Array ( [seconds] => 8 [minutes] => 1 [hours] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [year] => 2012 [yday] => 162 [weekday] => Monday [month] => June [0] => 1339455668 )
DateTime Object ( [date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America/Los_Angeles )
new DateTime('now');
http://www.php.net/manual/en/datetime.construct.php
Comparing is easy:
$today = new DateTime('now');
$newYear = new DateTime('2012-01-01');
if ($today > $newYear) {
}
Op's edit
I just needed to call date_default_timezone_set, and then this code worked for me.
To get difference in days use this:
$today = new DateTime('today');
the time in this object eill be 00:00:00
If you want difference with hours minutes and seconds use this:
$now = new DateTime('now');
I ended up using the date_create constructor (no parameter) to get the current date.
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.
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.