PHP Time Problem - php

well, I"ve got a time format of the type: 2011-02-16T01:25:50+04:00
I want to strip the +04:00 from the end of the string. How to do it?
I am converting a timestamp from mysql to ISO 8601 format using this function:
$timeagotime = date('c',strtotime($row['created_at']));
where $timeagotime contains the value: 2011-02-16T01:25:50+04:00

Refer to the documentation for php's date function: http://php.net/manual/en/function.date.php
You need to use a different format string. For instance,
date('Y-m-d',strtotime($row['created_at']));
Givens you "2011-02-16". There are a number of format strings... check out the doc.

You could use substr() to remove it, or just generate a date without it in the first place, though you couldn't use the 'c' formatting option, since that'd just add the timezone on again.
$timeagotime = substr($timeagotime, 0, 19)
$timeagotime = date('Y-m-dTG:i:s', strtotime(...));

If you absolutely don't need the end then you can use the following. Otherwise I'd suggest using a different way to generate your date from the table. PHP Date
$timeagotime = substr(date('c',strtotime($row['created_at'])), 0, 19);

Related

PHP mktime notice

I want to change given date and time or date only into Unix time.
I tried like this:
mktime("Jan-12-2012 2:12pm");
But it’s not working:
Even in PHP documentation I looked at many examples and many of them don’t consist the matter that I want.
And when I try:
$user_birthday=$_POST["user_birthday"];
$db_user_birthday=empty($user_birthday)?"":mktime($user_birthday);
$_POST["user_birthday"] was given value from form that is jan-12-2012 2:12pm
it show error like this:
Notice: A non well formed numeric value encountered in C:\Program
Files (x86)\Ampps\www\admin\index.php on line 76
How do I fix it or display time into Unix?
Use this one:
date("M-d-Y h:i:s", strtotime($user_birthday));
You should be using strtotime instead of mktime:
Parse about any English textual datetime description into a Unix
timestamp.
So your code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
Then you can process that date like this to get it formatted as you want it to:
echo date("M-d-Y h:ia", $db_user_birthday);
So your full code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
echo date("M-d-Y h:ia", $db_user_birthday);
Note I also added spaces to your code in key points. The code will work without the spaces, but for readability & formatting, you should always opt to use cleaner code like this.
You should take a look at this answer: convert date to unixtime php
Essentially, you have mixed up mktime() with strtotime(). strtotime() allows you to parse an English textual string into a Unix timestamp. mktime() constructs a unix datetime based on integer arguments.
For example (again taken from the question above)
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
echo strtotime("2009-11-03 11:24:00PM");
1257290640

PHP format date from returned data

I have a query that is returning a grid. One of the columns brings back a column with a date, like this:
echo "<td>{$Row[ETA]}</td>";
This displays the ETA from the database like this:
2013-10-30 20:00:0
I basically want to remove the TIME portion and just keep the date. Can this be done in the TD or do I have to the conversion elsewhere? I would like to just do the conversion within the cell, if possible.
Let me know how this can be done.
You can use the strtotime() and date() functions to achieve this!!!
date("Y-m-d", strtotime($Row[ETA]));
Well! if you want to get just date then you should use this function in your query
DATE(date_field)
as this will return only date from the datetime column
Ideally you should listen to the suggestion by JohnConde because it will limit your overhead between the database and your script but you can also substr() on the fly if you wish like this:
echo "<td>".substr($Row['ETA'], 0, 10)."</td>";
You have to echo it differently:
$eta = $Row['ETA'];
$etaDate = date("Y-m-d", strtotime($eta));
//now use $etaDate
You can use the date function to format the time, as what you're getting is a date as a string, you can use strtotime.
I think the format you're looking for is: date("Y-m-d", strtotime($Row["ETA"]));, you can either parse that into a variable and save it there, or you can concatenate the results together for the final string.

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)

Converting MYSQL timestamp to ISO-8601 without timzone offset

I'm trying to use timeago ( http://timeago.yarp.com/ ) and have found solutions for converting timestamps from MYSQL using php to ISO-8601.
date('c',strtotime($TimeStamp));
This works fine except im getting the timezone offset at the end
2011-07-10T08:46:50-**05:00**
what I want is 2011-07-10T08:46:50Z
Does anyone have a solution or know why i'm getting the timezone offset?
You can do it directly in MySQL:
SELECT DATE_FORMAT(yourfield, '%Y-%m-%dT%H:%i:%s0Z')
There is a GET_FORMAT(datetime, 'iso') call as well, but that returns the format string for ISO 9075, which is not quite what you want. Since it doesn't do 8601 directly, you have to build the format string yourself.
The 'c' will return the entire date, including the timezone offset. You will have to build it manually using the other options. Try this:
$time_stamp = time();
echo date('o-m-N',$time_stamp)."T".date('H:i:s',$time_stamp)."Z";
http://php.net/manual/en/class.datetime.php#datetime.constants.types
const string ISO8601 = "Y-m-d\TH:i:sO";
You can use your own format:
date('Y-m-d\TH:i:s\Z',strtotime($TimeStamp));
Simple for Open Graph
date('Y-m-d\TH:i', strtotime('2015/09/24 08:46:50))
Another way:
substr(date('c',strtotime($Timestamp)),0,-6).'Z';

Manipulating Dates in PHP without using Regular Expressions

Is there a way to change a date from
1985-12-15
to
1985-12
without using a regular Expression?
<?php echo date('Y-m', strtotime('1985-12-15')); ?>
That should do it.
This will, using strtotime, convert 1985-12-15 to a unix timestamp. The date function then takes a second parameter timestamp on which to format the date.
Convert the date to time via strtotime then use date to output in correct date format, like so:
<?php
echo date('Y-m', strtotime('1985-12-15'));
Maybe I'm just stupid, but if you only want the beginning of that date, stored as a string, can't you just use substr to extract the 7 characters at the beginning of that string ?
A bit like this, for instance :
$input = '1985-12-15';
$output = substr($input, 0, 7);
var_dump($output);
Which does give you :
string '1985-12' (length=7)
No need for any date-manipulation related function, in this case -- and this will probably be even faster/cheapier that parsing the string to a date and all that.
(Yeah, I know, premature optimisation ^^ )
$myDate = date('Y-m',strtotime('1985-12-15'));
echo $myDate // prints '1985-12'
If you think a regular expression isn't "cheap", then time functions will almost certainly be even more expensive: you would need to convert the string into a time value, then format it back into a string...
do you have that time value already in a array or do you whant such a like output come from the date function?
If you are not sure how the date will be formated, you should use the strtotime function, otherwise its probably as easy to do if the format is yyyy-mmm-dd or yy-m-d.
$datearray = explode('-',$date);
echo $datearray[0].'-'.$datearray[1];
not best to substr($date,0,strrpos($date,'-'));?
that would be cheapest?

Categories