PHP date function does not print correct format - php

Can any one please help me to find out that what is wrong with following date() function format ??
$advised_time=date("D, d/n/Y", strtotime($this->input->post('advised_sign_on_date')));
The post variable $this->input->post('advised_sign_on_date') contains the date like : "11-12-2014"
when I print it shows the date format something like Thu, 11V12V2014. However, the format is fine but I do not understand why is V coming in instead of /.
Update
I figured out that this is happening because of json_encode. I was printing the json_encode array when I print the $advised_time it shows me correct format but I json_encode it escapes the slashes i guess. How can I avoid to do so ?

use default_timezone_set to set default timezone
date_default_timezone_set('your time zone');
find your time zone on:
click here

Related

How to split date and time value to a time

2016年03月12日 9時00分
This is returned by $this->input->post(date_time).I want to show only 0900. How to get this output by above one?
There is some unclear charectors in your date string, If the date string is in correct format you can use the below code to get only time part.
$dd = "2016-03-12 9:00";
echo date("Hi",strtotime($dd));
May be this will help you

How to format an UTC date to use the Z (Zulu) zone designator in php?

I need to display and handle UTC dates in the following format:
2013-06-28T22:15:00Z
As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:
$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00
Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?
No, there is no special constant for the desired format. I would use:
$date->format('Y-m-d\TH:i:s\Z');
But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.
If you are using Carbon then the method is:
echo $dt->toIso8601ZuluString();
// 2019-02-01T03:45:27Z
In PHP 8 the format character p was added:
$timestamp = new DateTimeImmutable('2013-06-28T22:15:00Z');
echo $timestamp->format('Y-m-d\TH:i:sp');
// 2013-06-28T22:15:00Z
In order to get the UTC date in the desired format, you can use something like this:
gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:
function dateTo8601Zulu(\DateTimeInterface $date):string {
return (clone $date)
->setTimezone(new \DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s\Z');
}
Edit: clone object before changing timezone.
Since PHP 7.2 DateTimeInterface::ATOM was introduced in favor of DateTimeInterface::ISO8601, although it still lives on for backward compatability reasons.
Usage
$dateTimeObject->format(DateTimeInterface::ATOM)

PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999

I am using Spreadsheet_Excel_Reader to read .xls files in PHP.
Everything goes fine until it comes to reading a date. If I am reading a date field, it will always return the date as Nov 30, 1999 (or variations of this date depending upon the format). I have tried setting the OutputEncoding and it's giving the same result. I tried dumping the 'example.xls' that comes with the library and that also produces the same result.
Any help on a workaround for this would be highly appreciated.
You don't need to format your date in excel...if you have a date format in mind wrap it with double quote. e.g "13/04/1987" format("DD/MM/YYYY");
Spreadsheet_Excel_Reader will read this as a normal string with the double quote wrapper around it.
Then in your PHP make a query to remove and replace double quote with nothing.
$var = "13/04/1987";
$removeQuote = str_replace('"','',$var);
After this you will have to replace every occurence of the forwardslash (/) with hypen(-).
$removeSlashes = str_replace('/','-',$removeQuote);
Then use the date function in PHP to format it to your suitability.
$format = date('Y-m-d', strtotime($removeSlashes));
echo $format;
And you'r done... full code goes below.
$var = "13/04/1987";
echo date('Y-m-d',strtotime(str_replace('/','-',str_replace('"','',$var))));

strftime() suddenly stopped working. Any insights?

I have a simple script which takes an input date, formats it, and stores it in the database. I was using the strftime function in the following way:
$pdate = strftime('Y-m-d', strtotime($_POST['post_date']));
For some reason, this suddenly started returning 'Y-m-d'. Yes, it was returning the format string I was passing it as the first argument! No date information at all. I also tried doing this by passing it a straight-up unicode timestamp as the second argument, but it still just returned the format string. It was working fine until a few days ago.
Now I've switched it to use the date() function instead:
$pdate = date('Y-m-d', strtotime($_POST['post_date']));
Everything works fine now! I'm just wondering if anyone has any ideas why the strftime() function suddenly stopped working. It seems really strange and it's going to bug me all day.
Actually the question might be opposite :) I don't know how did it return correctly formated date, because generally it should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
As it's described here :)
You are using wrong Date format in strftime parameter. Check strftime function. It should be like this:
$pdate = strftime('%Y-%m-%d', strtotime($_POST['post_date']));
and there are many other formats as well.

PHP convert time from UTC (TZD) format to "normal" time

I have a feed that gives me times (not datetimes) in this format: "20:00:00.000+03:00"
I need to convert it to display just "20:00"
I know that I can do it using substr and cutting out the rest of the string, but that seems more like a hack and makes me wonder if it can produce bad results.
I already tried doing date('H:m', strtotime("20:00:00.000+03:00")); but it gives me 20:10 ??
You are doing it right, except for the first argument :
date('H:i', strtotime("20:00:00.000+03:00"));
Will output the result you are expecting for.
Not H:m, but H:i. check the date format.

Categories