php change the string into time format - php

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.

A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output

You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');

$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php

Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );

Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

Related

How to correctly compare dates in php with the same format

Trying to do a simple date comparison. Both dates are in the format of "Y F jS, g:i a". e.g. 2018 September 3rd, 9:30 am.
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if($today>$expire){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}
The code I have doesn't work correctly. I also tried strototime() but that was also unsuccessful.
Try this.
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($today_dt > $expire_dt)
{ /* Do something */ }
Update: It might possible that the date string is not supported by DateTime parser so you can initialize your dates like this.
$today_dt = DateTime::createFromFormat('Y F jS, g:i a', $today);
$expire_dt = DateTime::createFromFormat('Y F jS, g:i a', $expire);
Use strtotime
$today = time();
$expire = strtotime(file_get_contents(dirname(__FILE__).'/maintenance/end_time.php'));
if($today>$expire){
//code
}
OR
$today = date('Y F jS, g:i a');
$expire = file_get_contents(dirname(__FILE__).'/maintenance/end_time.php');
if(strtotime($today) > strtotime($expire)){
unlink(dirname(__FILE__).'/maintenance/end_time.php');
}

Add 1 Week in DateTime to Create Expiry DateTime

I'm Getting Saved Date/Time from Database in Following Format,
$date_time = date("j M, Y, g:i a"); //1 Feb, 2015, 12:00 am
Looking for a solution to add 7 days in $date_time to create expiry date, Tried several solutions discused on stackoverflow but none of them is working for me,
Any help will be appriciated. Thanks
Edited
#panther answered partially worked with only Date
$date_time = date("j M, Y");
but with Date/Time
$date_time = date("j M, Y, g:i a");
adding 7 days
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));
will return the result "31 Dec, 1969, 12:00 am"
So I tried with only Date to calcualte the difference (Inside PHP While Loop)
<?php if ($end_date>$date_time) { ?>
Expired
<?php } else { ?>
Active
<?php } ?>
And it didn't work, either it sets all records to Active or Expired,
So I'm Back to Sqaure 1.
Note: I tried with UNIX_TIMESTAMP but the end result same, either sets all records to Active or Expired.
Try this:
$date_time = date("j M, Y, g:i a");
$end_date = date("j M, Y, g:i a", strtotime($date_time . ' + 7 days'));
Using DateTime is also a good option:
$date_str = "..."; // from database
$date_format = "j M, Y, g:i a";
$expire_date = DateTime::createFromFormat($date_format, $date_str);
$expire_date = $expire_date->add(DateInterval::createFromDateString('+7 days'));
and if you need it formated back to string:
$expire_date->format($date_format);
You could try using PHPs DateTime Object to get the current Time
$date_time_obj = new \DateTime();
$date_time = $date_time->format('j M, Y, g:i a');
And then use the add() method to get your expiry time
$end_date_obj = clone $date_time_obj;
$end_date_obj = $end_date_obj->add(new \DateTimeInterval('P7D'));
$end_date = $end_date_obj->format('j M, Y, g:i a');
A little more complicated than the other answers, i know :) But maybe also a little more helpfull in the future!
Cheers

Regex to convert already formatted date to different format

I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?
You should use the DateTime class.
$date = new DateTime($run['at']);
echo $date->format('d M Y, H:i');
You can use:
$time = strtotime($run['at']);
echo date("Y-m-d", $time);
But, where does $run['at'] come from?

PHP date conversion dd [th/st/rd] / month / yyyy

I have my users entering the date in this format :- mm/dd/yyyy (11/21/2012)
My PHP script converts the date into the following format :- dd-Month-yyyy (21-November-2012)
I do this using :-
$new_date = date('d-F-Y', strtotime($user_date));
How can I have the date in this format :- 21st November 2012?
Thanks
You can use S letter as following:
$new_date = date('jS F Y', strtotime($user_date));
Check manual.
It will output as you expect
$my_date = '2016-01-01';
echo date('F jS, Y', strtotime($my_date));
# January 1st, 2016
while dS will also prepends 0
echo date('F dS, Y', strtotime($my_date));
# January 01st, 2016
$new_date = date('jS F Y', strtotime($date));
S - English ordinal suffix for the day of the month, 2 characters
(st, nd, rd or th. Works well with j)
**My Date = 22-12-1992**
<?php
$mydate = "22-12-1992";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
**OutPut = 22nd December 1992**
You can use something like:
echo date('l, F jS');
Or even get a bit fancy with the HTML:
echo date('l, F j<\s\u\p>S</\s\u\p>');
You can do that :
<?php echo date('dS M. Y');?>
$date = date_create('09-22-2012');
echo $date->format('d S F Y');
by this code you will get your desire
for more you can also visit http://php.net/manual/en/datetime.formats.date.php

PHP Convert a Date into this format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP convert one date into another date format
This is PHP
I have this result:
2011-09-20 13:00:00
I want to convert it into this format:
September 20 2011 1:00 pm
Do you know how to do that?
Anyhelp would be greatly appreciated.
Thanks!
try this:
$old_date = '2011-09-20 13:00:00';
$old_date_timestamp = strtotime($old_date);
$new_date = date('F j Y g:i a', $old_date_timestamp);
If that result comes from an object of type DateTime you can use the format function:
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
and here all the formats you can have.. change it according to your needs.
You can get the UNIX-timestamp of a time string with strtotime() and you can get a differently designed time string with strftime()
For more information you can read the documentation here : http://php.net/manual/en/function.date.php
But also is simple. Lets see how
$d = "2011-09-20 13:00:00";
$d = strtotime($d);
$d = date("F m Y g:i a", $d);
echo $d;
Take a look at these PHP functions:
http://nl.php.net/manual/en/function.date.php
and
http://nl.php.net/strtotime
To do something like this:
date('F d Y G:i',strtotime("2011-09-20 13:00:00")); // your required format
You can use this format
$date= date("F j Y g:i a");
if you have date in any variable then
$date= date("F j Y g:i a",strtotime($your_variable));
echo $date
echo date("F d Y g:i a",strtotime("2011-09-20 13:00:00 "));
echo date('F d Y g:i a', strtotime('2011-09-20 13:00:00'));
check the date format

Categories