Change date y-M to Y-m in php - php

I want to make date from 11-Mar to 2011-03. But I think php is thinking 11-Mar is 2012-Mar-11. How can i do it?
value : 11-Mar
expect : 2011-03
result : 2012-03 [date(strtotime('11-Mar'))]

If you are using PHP >= 5.3.0 Then you can use this...
$date = date_create_from_format('y-M', '11-Mar');
echo date_format($date, 'Y-m');
http://www.php.net/manual/en/datetime.createfromformat.php

You can do this using the DateTime class present in php.
$str = "11-Mar";
$date = DateTime::createFromFormat('y-M', $str);
echo $date->format('Y-m');
http://www.php.net/manual/en/datetime.createfromformat.php
Regards,

$result = date ('Y-m',strtotime('2011-Mar'));
echo $result;

Try
$date= date('Y-m',strtotime('2011 Mar'));
echo $date;

The above is good, this is more exact for what you need to convert FROM:
<?php
$a = "11-Mar";
$date = explode("-", $a);
if ($date[0] <= 99)
$year = '19'.$date[0];
else
$year = '20'.$date[0];
$mon = $date[1];
$date= date('Y-m',strtotime("$year $mon"));
echo $date; // echoes 2011-03
?>

Related

Compare Dates PHP

I want to only shows result for the current month, but I have no idea how to do this, my current output is like this.
Output
2014-04-11
-> array
2014-04-11
2014-04-05
2014-03-29
PHP
$date = date('Y-m-d');
echo $date;
echo "<pre>";
foreach ($submissions as $test){
if($date >= substr($test['thing']['created'], 0, 10)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
My current code wont work as its only checking if the whole number is greater or equal to, any ideas anyone?
Just try with strtotime:
$year = date('Y');
$month = date('m');
foreach ($submissions as $test) {
$timestamp = strtotime($test['thing']['created']);
$testYear = date('Y', $timestamp);
$testMonth = date('m', $timestamp);
if (($month >= $testMonth && $year == $testYear) || $year > $testYear) {
// test passed
}
}
If your problem is to have only results of current month :
$date = date('Y-m');
echo "<pre>";
foreach ($submissions as $test){
// if month & year is equal
if($date == substr($test['thing']['created'], 0, 7)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
If you want to sort it, you will have to convert to timestamp
i'm not a pro in php but take a look here : http://fr2.php.net/manual/fr/function.date.php
does date('m') would be a solution for your problem?
You can try 2 ways :
convert them to timestamp, then compare the int
If you are taking the dates frome a DB, ORDER BY date in the query
Variable for current month first $cur_m=date('m')
Explode your variable in loop with -.
$p = explode('-',$test['thing']['created']);
in loop check for current month like below
if($p[1]==$cur_m){ your code... }

I have Week and year in php I need to convert date complete

I have Week and year in php I need to convert date complete, I'm new in php help please
<?php
$weekY = date('Wy' ,strtotime('-1 week'));
print_r(date("Y-m-d",strtotime($weekY)));
?>
the result is 1970-01-01
and I have the 5113(Wy) in my database how to convert in Wy
<?php
$weekY = date('Wy' ,strtotime(5113));
echo $weekY."<br/>";
?>
the result is 0170
I read examples but do not help me in my problem
Make use of setISODate
<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52);//year and week
echo $gendate->format('d-m-Y'); //"prints" 23-12-2013
thanks for everything, and I have the answer to my question
<?php
$y = substr('5213',-2);
$Y = 20..$y;
$W = substr('5213',0,2);
echo "Year + Week: ".$Y." ".$W."<br/>";
$date = new DateTime();
$date->setISODate($Y, $W);
echo "Result: ".$date->format('Y-m-d') . "\n";
?>

PHP: Date larger than current date

I have this code:
$curdate = '22-02-2011';
$mydate = '10-10-2011';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This would echo expired BUT shouldn't because $mydate is in the future and therefore smaller than the $curdate but PHP is looking at JUST the first two numbers 22 and 10 instead of the whole string. How can I fix this?
Thanks
Try converting them both to timestamps first, and then compare two converted value:
$curdate=strtotime('22-02-2011');
$mydate=strtotime('10-10-2011');
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}
This converts them to the number of seconds since January 1, 1970, so your comparison should work.
The problem is that your current variables are strings, and not time variables.
Try this out:
$curdate = strtotime('22-02-2011');
$mydate = strtotime('10-10-2011');
$row_date = strtotime($the_date);
$today = strtotime(date('Y-m-d'));
if($row_date >= $today){
-----
}
$currentDate = date('Y-m-d');
$currentDate = date('Y-m-d', strtotime($currentDate));
$startDate = date('Y-m-d', strtotime("01/09/2019"));
$endDate = date('Y-m-d', strtotime("01/10/2022"));
if (($currentDate >= $startDate) && ($currentDate <= $endDate)) {
echo "Current date is between two dates";
} else {
echo "Current date is not between two dates";
}
Use the PHP date/time classes to convert these string representations into something you can directly compare using getTimestamp() to compare the UNIX times.
If you're sure all your dates are in this format, you can string slice them into YYYY-MM-DD, and a string comparison will function correctly then.
if(strtotime($curdate) > strtotime($mydate))
{
...
}
it's VERY simple
$curdate = '2011-02-22';
$mydate = '2011-10-10';
if($curdate > $mydate)
{
echo '<span class="status expired">Expired</span>';
}

Get the year from specified date php

I have a date in this format 2068-06-15. I want to get the year from the date, using php functions. Could someone please suggest how this could be done.
$date = DateTime::createFromFormat("Y-m-d", "2068-06-15");
echo $date->format("Y");
The DateTime class does not use an unix timestamp internally, so it han handle dates before 1970 or after 2038.
You can use the strtotime and date functions like this:
echo date('Y', strtotime('2068-06-15'));
Note however that PHP can handle year upto 2038
You can test it out here
If your date is always in that format, you can also get the year like this:
$parts = explode('-', '2068-06-15');
echo $parts[0];
I would use this:
$parts = explode('-', '2068-06-15');
echo $parts[0];
It appears the date is coming from a source where it is always the same, much quicker this way using explode.
You can try strtotime() and date() functions for output in minimum code and using standard way.
echo date('Y', strtotime('2068-06-15'));
output: 2068
echo date('y', strtotime('2068-06-15'));
output: 68
public function getYear($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("Y");
}
public function getMonth($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("m");
}
public function getDay($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("d");
}
<?php
list($year) = explode("-", "2068-06-15");
echo $year;
?>
You can achieve your goal by using php date() & explode() functions:
$date = date("2068-06-15");
$date_arr = explode("-", $date);
$yr = $date_arr[0];
echo $yr;
That is it. Happy coding :)
Assuming you have the date as a string (sorry it was unclear from your question if that is the case) could split the string on the - characters like so:
$date = "2068-06-15";
$split_date = split("-", $date);
$year = $split_date[0];
$Y_date = split("-","2068-06-15");
$year = $Y_date[0];
You can use explode also
You wrote that format can change from YYYY-mm-dd to dd-mm-YYYY you can try to find year there
$parts = explode("-","2068-06-15");
for ($i = 0; $i < count($parts); $i++)
{
if(strlen($parts[$i]) == 4)
{
$year = $parts[$i];
break;
}
}

PHP Parse Date String

If I've got a date string:
$date = "08/20/2009";
And I want to separate each part of the date:
$m = "08";
$d = "20";
$y = "2009";
How would I do so?
Is there a special date function I should be using? Or something else?
Thanks!
One way would be to do this:
$time = strtotime($date);
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);
explode will do the trick for that:
$pieces = explode("/", $date);
$d = $pieces[1];
$m = $pieces[0];
$y = $pieces[2];
Alternatively, you could do it in one line (see comments - thanks Lucky):
list($m, $d, $y) = explode("/", $date);
Check out PHP's date_parse function. Unless you're sure input will always be in a consistent format, AND you validate it first, it is much more stable and flexible, (not to mention easier), to let PHP try to parse the format for you.
e.g.
<?php
//Both inputs should return the same Month-Day-Year
print_r(date_parse("2012-1-12 51:00:00.5"));
print_r(date_parse("1/12/2012"));
?>
If you have a given format you should use a date object.
$date = DateTime::createFromFormat('m/d/Y', '08/20/2009');
$m = $date->format('m');
$d = $date->format('d');
$y = $date->format('Y');
Note you can certainly use only one call to DateTime::format().
$newFormat = $date->format('d-m-Y');
Is it always like that? Or will it be in any sort of file format?
Try strtotime.
Something like:
if(($iTime = strtotime($strDate))!==false)
{
echo date('m', $iTime);
echo date('d', $iTime);
echo date('y', $iTime);
}
how about this:
list($m, $d, $y) = explode("/", $date);
A quick one liner.
For internationalized date parsing, see IntlDateFormatter::parse - http://php.net/manual/en/intldateformatter.parse.php
For example:
$f = new \IntlDateFormatter('en_gb', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
$dt = new \DateTime();
$dt->setTimestamp($f->parse('1/2/2015'));
Dominic's answer is good, but IF the date is ALWAYS in the same format you could use this:
$m = substr($date,0,2);
$d = substr($date,3,2);
$y = substr($date,-4);
and not have to use an array or explode
Bill H

Categories