Maybe a trivial question but I can't seem to figure this out. I need to format a date like 2013-11-01T08:50:43.305Z in PHP. I'm not sure how to add .305Z-part in the formatting. I've been looking at the docs (php's date-function documentation) but I must have overread it there... Any help is appreciated.
use this:
echo date("c");
For more reference see here: http://php.net/manual/en/function.date.php
Or you can use this format:
echo date("Y-m-dh:i:sz");
Here Z means the number of day in the current year.
Related
In Javascript to get the current time for the time stamp I'm using
JSON.stringify(new Date());
Which is giving me following output.
"2016-06-07T10:38:54.767Z"
In PHP how can I get this kind of output in datetime function.
I know this problem can be handled by date function of php. But what I'm looking for is exact argument to that date function so that I can get the output same as given in the question. Which is in UTC timezone. There are similar questions explaining date function but I'm not able to figure out exact arguments for my desired output.
In PHP I tried this
date_default_timezone_set('UTC');
echo date('c');
Which is giving me
2016-06-07T11:56:54+00:00
Thanks in advance.
Reading from the DateTime Class php.net documentation,
You can use $yourdate->format('c')
http://php.net/manual/en/datetime.format.php
See here for date format details :
http://php.net/manual/en/function.date.php
I'm new to PHP and I need a PHP function that converts Linux times like 1396310400 from $variable and returns a human-readable date like 1. apr. 2014! (time of day not needed)? Any suggestions is much appreciated.
echo date('d/m/Y', $time_linux);
read here: http://php.net/manual/en/function.date.php to learn how to modify the result to show exactly what you want.
Thanx for your input.
This worked and returned a human readable date into my wpallimport run - at first I just did not understand the syntax:
function overtagelsesdato($time_linux) {
echo date('d/m/Y', $time_linux);
}
thank you OfirH!
<?php echo invoice_due_date($invoice); ?>
The above code outputs a date that is formatted as dd/mm/yyyy. I am looking for a way to take that date and then add or remove x amount of days from it and then print the result. I'm a novice when it comes to PHP so everything I have tried has failed. Thanks in advance!
Also, this code snippet is from a no longer supported project called "myclientbase" if it helps.
PHP5 has a nice class called DateTime.
You can initialize it from a string like this:
$date = DateTime::createFromFormat('d/m/Y', invoice_due_date($invoice));
Then, since PHP 5.3.0 this class has a method to add time amounts:
$date->add(new DateInterval('P10D'));
This adds 10 days to your date. See http://php.net/manual/en/datetime.add.php.
strtotime can translate the date to a time value (although you'd have to replace the slashes with dashes) and in the same operation add days:
strtotime(str_replace('/','-',invoice_due_date($invoice)) . ' + 1 day')
date can be used to format the date back to a suitable notation.
You can use the DateTime::add function. A full explanation and examples can be found at http://php.net/manual/en/datetime.add.php
$invoice->add(new DateInterval('P20D'));
This would add 20 days to the invoice date. You can simply run this before echoing out your $invoice variable.
I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>
Is there an easy way to get the correct format for an iCal DTSTART using php date?
I need the format to look like: 20111008T110000 or 20111008 (that one is easy) if I don't have any time.
Does PHP date have a quick way to do this, specifically one that adds the time or removes it when needed?
There isn't any native PHP function or date format that I'm aware of, so you'll need to create your own function. Something like this:
function getIcalDate($time, $inclTime = true)
{
return date('Ymd' . ($inclTime ? '\THis' : ''), $time);
}
As hakre pointed out in a comment, the date formatter can't distinguish between a date with time and a date without time - you'll have to decide the logic behind that.
date('Ymd\THis', time())
You can replace time() with your own timestamp.
This is might be you are looking for link and this also can help you.
mktime function to make timestamp as you want including time or only date to it.