I want to get the current date as a string with leading zeros, means:
$date = date("Y-n-j", time());
gives me eg: 2014-9-12, but what I want is this: 2014-09-12
Here is a list of all the things you can do to format dates with date()
$date = date("Y-m-d");
Please read this: http://php.net/manual/en/datetime.format.php
<?php
$date = date("Y-m-d", time());
echo $date;
?>
Related
I trying to format string to date. String looks like that: 20200219 and I want to format it like 2020-02-19. Can some help me with this?
You have multiple options:
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$changed_date = "20200219";
echo date("Y-m-d", strtotime($changed_date ) );
$time = strtotime('03/05/2020');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2020-03-05
You need to be careful with m/d/Y and m-d-Y formats. PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:
$ymd = DateTime::createFromFormat('m-d-Y', '03/05/2020')->format('Y-m-d');
Another Option:
$d = new DateTime('03/05/2020');
$timestamp = $d->getTimestamp(); // Unix timestamp
$formatted_date = $d->format('Y-m-d'); // 2020-03-05
you can do like that
$s = '20200219';
$date = strtotime($s);
echo date('Y-m-d', $date);
if it's a string you could do like this:
$date=date_create("20200219");
return date_format($date,"Y-m-d");
Hope it helped.
Try this one:-
$var = "20200219";
echo date("Y-m-d", strtotime($var) );
You can try php way:
date("Y-m-d", strtotime("20200219") );
$dates = '2019-11-15 11:25:12';
$date = date('d-m-Y', strtotime($dates));
current output: 15-11-2019
expected output: 15-Nov-2019
In this code I am have datetime and I want to change month number to name between date and year. So, How can I do this? Please help me.
Thank You
You just have to change the date format from 'd-m-Y' to 'd-M-Y'.
All date format letters are explained here for future reference: https://www.adminschoice.com/php-date-format
You could try to change format string:
$dates = '2019-11-15 11:25:12';
$date = date('d-M-Y', strtotime($dates));
echo $date;
Use uppercase M or F for month
M is the first three letter while F is full name of the month
$date = date('d-M-Y', strtotime($dates));
or
$date = date('d-F-Y', strtotime($dates));
I have a string in the format YYYYMMDDHH24MISS that is year, month, day, hours, minutes, seconds. I want to convert this to a date, add one day to it and return it in the same format. Sounds simple but I am unable to get this to work. I have tried a number of different ways where $field3 contains the date string for example:
$end_date = strtotime(substr($field3,1,8));
$date_interval = DateInterval::createFromDateString('1 day');
$new_end_date = date_add($end_date, $date_interval);
$field3 = ($new_end_date->format('YYYYMMDD')).substr($field3,8,6);
In this example $new_end_date contains "false".
Example date time string: 20170912124159 being 12/09/2017 12:41:59
The format of your input string can be parsed by the constructor of class DateTime (and date_create() and strtotime()) without problems.
$date = new DateTime('20170912124159');
$date->add(new DateInterval('P1D'));
echo($date->format('Y-m-d H:i:s'));
# The output is:
# 2017-09-13 12:41:59
You can, as well, format the date as string using the format YmdHis to get the modified date in the same format as the input string.
echo($date->format('YmdHis'));
# 20170913124159
Read about DateTime and DateInterval.
You can try something like this:
$date = DateTime::createFromFormat('YmdHis', '20170912131313');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d');
For more information: http://php.net/manual/en/datetime.add.php
Please try this
$date = date("Y/m/d H:i:s"); // or '2017/09/30 20:24:00'
$ndate = date('Y/m/d H:i:s', strtotime($date . ' +1 day'));
echo 'date after adding 1 day: ' . $ndate;
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.