I have a date variable like 10-25-1998 and I only want to echo 10. Can anyone help me about it? Thanks...
$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
echo date('m', $dateString); // not working here....will print out 01
strtotime will not recognize "m-d-Y" format, so the only way to get the month out of that string is to just parse it yourself, knowing that it's the first value in your string:
$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
$t = explode('-',$dateString);
echo $t[0]; //month
echo $t[1]; //day
echo $t[2]; //year
$tomorrow = mktime(0,0,0,01,25,1998);
echo "Month is ".date("m", $tomorrow);
For more info look here: PHP Date() Function
$first2chars = substr($tomorrow, 0, 2);
picks the first 2 characters of the string $tomorrow
take care
You may want the getdate function:
$today = getdate();
echo $today["mon"];
Related
I have a string that contains date and time. Format of my string is yyyymmddtime.
For example. 20171125123000209. this is my complete string in which first comes the month, then month and then day after that time. How can i retrieve date from it by converting to readable date format. I tried with php's date function. But the output was not as expected. Please help.
Try this
<?php
$str_date= "20171125123000209";
$exiting_date_format='Ymd';
//first 8 characters from given date string in second parameter below
$date = DateTime::createFromFormat($exiting_date_format, substr($str_date,0,8));
echo $date->format('Y-m-d');//specify desired date format
?>
Output :
2017-11-25
DateTime::createFromFormat - Parses a time string according to a specified format
You can use date() function of php
<?php
$full_date= "20171125123000209";
echo date('dS F h:i:s A', $full_date);
//Output = 31st May 12:30:09 AM
?>
Or,
To get date from timestamp like now,
$timestamp= time(); //Or your timestamp here
$date = date('d-m-Y', $timestamp); //Inside first parameter, give your date format
echo $date; //17-12-2017
Or,
To get anything from string you can also use substr() function of php.
<?php
$full_date= "20171125123000209";
$year = substr($full_date, 0, 4);
$month = substr($full_date, 4, 2);
$date = substr($full_date, 6, 2);
echo 'Year = '.$year.' ';
echo 'Month = '.$month.' ';
echo 'Date = '. $date.' ';
?>
Output:
Year = 2017 Month = 11 Date = 25
Test in jdoodle
About substr() function in php
It's not necessary to manipulate the string at all. PHP's DateTime class supports parsing a string containing miliseconds natively, using the u format modifier:
$str = '20171125123000209';
$date = DateTime::createFromFormat('YmdHisu', $str);
Using your new $date object, you can convert to whatever format you're looking for, e.g.
echo $date->format("F j Y, g:i a");
// November 25 2017, 12:30 pm
See https://eval.in/920636
$your_strtotime_val = ""; //20171125123000209
$convert_to_date = date("m-d-Y h:i:s",$your_strtotime); // [m-d-Y h:i:s] this depending on how you convert date in first time so be careful
how to get "Year-Month" from "2014-11-01"
i can get those with following function
$date = "2014-11-01";
$year = date('Y', strtotime($date));
$month = date('F', strtotime($date));
echo $year.'-'.$month;//return 2014-11
OR
echo substr($this->getValue('PayMonth'),0,7);// return 2014-11
Now My question is can we do this using date() or any other PHP build-in function in one line
for example
echo date('Y-m','2014-11-01');
of course. Try this
echo date('Y-m',strtotime('2014-11-01'));
Try with strtotime
echo date('Y-m',strtotime('2014-11-01'));
You can also do with explode like
$date_array = explode('-','2014-11-01');
echo $date_array[0].'-'.$date_array[1];
But make sure that your date format will be like "YY-MM-DD", if you want to use explode.
I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015
I have a date which is formatted simply as 19830210 (YYYYDDMM).
How would I reorder it to 02101983 (DDMMYYYY) using php?
I simply want to rearrange the digits in the date.
use createFromFormat(), do:
$date = "19830210";
$newFormat = DateTime::createFromFormat('Ydm', $date);
echo $newFormat->format('dmY'); //will give you 02101983
echo $output=date('dmY',strtotime('02101983'));
For future use with date format use below link for reference
http://php.net/manual/en/function.date.php
use substr() function:
<?php
$date1 = 'YYYYDDMM';
$date2 = substr($date1 , 4 , 4).substr($date1 , 0 , 4);
echo $date2;
?>
$str='19830210'; //(YYYYDDMM).
echo $str."<br/>";
$yy=substr($str,0,4);
$mm=substr($str,4,2);
$dd=substr($str,6,2);
$reorder=$dd.$mm.$yy;
echo $reorder; //(DDMMYYYY).
I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?
Thanks,
Jonesy
I used date_format($date, "m"); //01, 02..12
This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1
See http://php.net/date
date('m') or date('n') or date('F') ...
Update
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
F Alphabetic representation of a month January through December
....see the docs link for even more options.
What does your "data variable" look like? If it's like this:
$mydate = "2010-05-12 13:57:01";
You can simply do:
$month = date("m",strtotime($mydate));
For more information, take a look at date and strtotime.
EDIT:
To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.
Alternatively, try one of these:
if((int)$month == 1)...
if(abs($month) == 1)...
Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime);
echo date('y', $unixtime );
as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so
echo date('n'); // "9"
As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.
<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";
// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);
// Output it month is various formats according to http://php.net/date
echo date('M',$dateAsUnixTimestamp);
// Will output Apr
echo date('n',$dateAsUnixTimestamp);
// Will output 4
echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>
To compare with an int do this:
<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
echo "They are the same";
}
?>