This question already has an answer here:
strtotime() with only year return wrong data
(1 answer)
Closed 2 years ago.
I have this PHP code:
<?php
$date = 2022;
$str = strtotime(2022);
echo $str; // 1613161320
echo '<hr/>';
echo date('Y', $str); // 2021
why this link date('Y', $str); is showing 2021 instead of 2022 ?
strtotime() expects a formated date string like 10 September 2000 see php docs.
When you want to get a unix timestamp by seperate year, month, day, hour, minute and second values you should have a look at mktime() see php docs.
So what you want to get the first 2022-date possible is:
$str = mktime(0, 0, 0, 1, 1, 2022);
Related
This question already has answers here:
How to reformat date in PHP?
(9 answers)
Closed 4 years ago.
I have sting like this:
"Last Updated Date is Feb 12, 2011"
I want to take only date value from this string in different format like
"Y-m-d"
strpos gives us the position upto is which 18, as we do not like to include is itself, we find its lenth using strlen. Using substr, we extract the data after the position from the word is. Since we don't want extra space, trim the result and Format it using createFromFormat:
$str = "Last Updated Date is Feb 12, 2011";
$mydate = substr($str, strpos($str, 'is')+strlen('is'));
$date = DateTime::createFromFormat('M d, Y', trim($mydate));
echo $date->format('Y-m-d');
a short version for extracting the date:
substr($str, -12);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
can somebody help me with my problem? I already figure out the problem but seems i do not now how to solve it without using explode(), so the problem is that the string date for example '02-15-17' i want to convert it in date() expected result (Feb 15, 2017) using strtotime() doesn't give me a right/exact year cause of the parameter 3 which is the year it has only 2 digit in HTML5 input tag date in chrome browser, can somebody help me to solve this problem? for the vision of my code here it is.
<?php
$user_date = '02-15-17';
echo date('M d, Y', strtotime($user_date));
?>
result:
Feb 2, 1917
You could also use a DateTime with the format function:
$user_date = '02-15-17';
$dateTime =DateTime::createFromFormat('m-d-y', $user_date);
echo $dateTime->format('M d, Y');
That will give you:
Feb 15, 2017
Output php
Since all your date are over 2000's use substr function try this
$olddate = "02-15-17";
$standarddate = substr($olddate,3,2) . "." . substr($olddate,0,2) . "." . "20" . substr($olddate,6,2);
echo date("jS F, Y", strtotime($standarddate)).' - '.$standarddate;
output : 15th February, 2017 - 15.02.2017
This question already has answers here:
Get month of a given date
(6 answers)
Closed 7 years ago.
i have a string in this format '2015-4-28'
How i get month number for this string ( the value in this example is 4)?
Thanks
Another option would be to do this:
date('m', strtotime($date));
Change the m to an n to get the month without the leading 0.
With this:
<?php
$a = "2015-4-28";
$parts= explode("-", $a);
echo $parts[0]; // 2015
echo $parts[1]; // 4
echo $parts[2]; // 28
?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date value that is being passed as 201501.
How could I take that and display January 2015 instead?
It's being passed through a PHP variable.
Just try with:
$input = '201501';
$output = DateTime::createFromFormat('Ym', $input)->format('F Y');
Try below code
$data = '201501';
$monthNum = substr($data,4);
$year = substr($data,0,4);
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
echo $monthName.' '.$year;
This question already has an answer here:
Parsing dates from string - regex
(1 answer)
Closed 8 years ago.
for example
$string = "Today is March 24 2014. My Birthday is on 3/24/2014";
how to extract those two date with different format?
Try this
<?php
$string = "Today is March 24 2014. My Birthday is on 3/24/2014";
$temp=explode('.',str_replace(' My Birthday is on ','',str_replace('Today is ', '', $string)));
echo date('Y-m-d',strtotime($temp[0]));
echo "<br/>";
echo date('Y-m-d',strtotime($temp[1]));
?>
Output
2014-03-24
2014-03-24
You need to use createFromFormat() to do this, and specify which format should be used for parsing.
If you are looking for a function that does not need a format specified, it won't work. Consider for example: 1/2/2014. Is it Februari first, or Januari second?
So assuming your day of the month doesn't get leading zeros when under 10, it would be:
$date1 = DateTime::createFromFormat('F j Y', 'March 24 2014');
$date2 = DateTime::createFromFormat('n/j/Y', '3/24/2014');
I know just how to extract some of the date from string,i use,this
try this then
preg_match('/(\d?\d [A-Za-z]+ \d\d\d\d) at (\d\d\:\d\d)/', $editdate, $matches);
print_r($matches);
$date = $matches[1];
$time = $matches[2];
this will work in every case,though the code looks lil ugly,
this can work well,for the first case,you can split the string first for the second case.