Store date directly in PHP variable - php

I want to store a specific date in a variable. If stored like $x="01/01/2016" it is acting as a string from which I cannot extract a part, like from getdate() year, month, day of the month, etc.

Use the DateTime object:
$dateTime = new DateTime('2016/01/01');
To get only parts of the date you can use the format method:
echo $dateTime->format('Y'); // it will display 2016
If you need to create it from the format you wrote in the question, then you can use the factory method createFromFormat:
$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/2016');
echo $dateTime->format('Y/m/d');

This is work for me
$date = '20/May/2015:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);

this is what you are looking for http://php.net/manual/en/class.datetime.php

You can use $myDate = new DateTime('01/01/2016'); to declare date. To get year, month and date from the specified date, use echo $myDate->format('d m Y');
Change the format based on your need. To know more about date format refer

Related

Convert date and time to only year in php

I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');

Get previous weekday from sql date string in PHP

I have a date string in sql format (eg 2017-08-05) and will like to convert it to get the previous weekday.
I know that there is the strtotime method where I can get the previous weekday from a specific date, such as:
date("Y m d",strtotime("-2 Weekday", strtotime("2017-08-05")));
Is there a better (prettier) way to do this? Or is this the PHP way?
The same can be achieved with just one call to strtotime:
date("Y m d", strtotime("2017-08-05 -2 Weekday"))
By using Carbon
Carbon::parse('2017-08-05')->previousWeekday()
Or if you want sameway
Carbon::parse('2017-08-05')->subWeekday(2)->toDateString()
you can create a carbon
carbon::parse('2012-08-05')->previousWeekday();
No better way then this...
Try this:
// create a new instance of DateTimeImmutable
$date = new \DateTimeImmutable('2017-08-05');
// create a one-day interval
$interval = new \DateInterval('P1D');
// subtract the interval, and keep doing that while the day before is not a weekday
do {
$date = $date->sub($interval);
} while (5 < $date->format('N'));
echo $date->format('Y-m-d');
For reference, see:
http://php.net/manual/en/class.datetimeimmutable.php
http://php.net/manual/en/class.dateinterval.php
http://php.net/manual/en/function.date.php
For an example, see:
https://3v4l.org/aT6Zj

How to get current date in codeigniter

How to get current date in codeigniter in YY-mm-dd format. I wants to get current date in YY-mm-dd frmat and put this value into input text box
You can use the PHP date function.
date('Y-m-d');
Up to my knowledge, there is no separate date function in codeigniter.
EDIT :
But if you want date in this format 13-04-05 [ yy-mm-dd ], Try this
date('y-m-d');
For more date formats, check this link PHP Date Formats
Try to use this is a generic format of DateTime
echo date('Y-m-d H:i:s');
use php date function
echo date("Y-m-d");
will give you the result
What about:
$date = new \Datetime('now');
var_dump($date);
if you want the full date:
echo date('Y-m-d');
depends your date structure.
echo date('d-m-Y');
if you want year only, then echo date('Y'); is enough
Use php date() function, like this
echo Date('Y/m/d');
it give you the desired result!

change dd/mm/yy date format to yy/mm/dd using php

I'm having date 20/12/2001 in this formate . i need to convert in following format 2001/12/20 using php .
$var = explode('/',$date);
$var = array_reverse($var);
$final = implode('/',$var);
Your safest bet
<?php
$input = '20/12/2001';
list($day, $month, $year) = explode('/',$input);
$output= "$year/$month/$day";
echo $output."\n";
Add validation as needed/desired. You input date isn't a known valid date format, so strToTime won't work.
Alternately, you could use mktime to create a date once you had the day, month, and year, and then use date to format it.
If you're getting the date string from somewhere else (as opposed to generating it yourself) and need to reformat it:
$date = '20/12/2001';
preg_replace('!(\d+)/(\d+)/(\d+)!', '$3/$2/$1', $date);
If you need the date for other purposes and are running PHP >= 5.3.0:
$when = DateTime::createFromFormat('d/m/Y', $date);
$when->format('Y/m/d');
// $when can be used for all sorts of things
You will need to manually parse it.
Split/explode text on "/".
Check you have three elements.
Do other basic checks that you have day in [0], month in [1] and year in [2] (that mostly means checking they're numbers and int he correct range)
Put them together again.
$today = date("Y/m/d");
I believe that should work... Someone correct me if I am wrong.
You can use sscanf in order to parse and reorder the parts of the date:
$theDate = '20/12/2001';
$newDate = join(sscanf($theDate, '%3$2s/%2$2s/%1$4s'), '/');
assert($newDate == '2001/12/20');
Or, if you are using PHP 5.3, you can use the DateTime object to do the converting:
$theDate = '20/12/2001';
$date = DateTime::createFromFormat('d/m/Y', $theDate);
$newDate = $date->format('Y/m/d');
assert($newDate == '2001/12/20');
$date = Date::CreateFromFormat('20/12/2001', 'd/m/Y');
$newdate = $date->format('Y/m/d');

Converting date to this format

I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);

Categories