Pergunta

i have a date '07/23/2009' and a time '18:11' and i want to get a timestamp out of it : here is my example:

date_default_timezone_set('UTC');

$d = str_replace('/', ', ', '07/23/2009');
$t = str_replace(':', ', ', '18:11');
$date = $t.', 0, '.$d;
echo $date;
echo '<br>';
echo $x = mktime("$date");

the issue is that $x gives me the current timestamp.

any ideas?

Foi útil?

Solução

it gives error because mktime function require all values of numbers only and this function gives only date . if you try like

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

then it will work.

so your complete code will be

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//if you want timestamp then use

echo strtotime("07/23/2009 18:11");

Thanks

Outras dicas

Use the DateTime class:-

$dateStr = '07/23/2009';
$timeStr = '18:11';
list($hours, $minutes) = explode(':', $timeStr);

$dateTime = \DateTime::createFromFormat('m/d/Y', $dateStr)->setTime($hours, $minutes);
$timeStamp = $dateTime->getTimestamp();

or:-

$dateStr = '07/23/2009 18:11';
$timestamp = \DateTime::createFromFormat('m/d/Y H:i', $dateStr)->getTimestamp();

Try using strtotime

$x = strtotime($date." ".$time);

for your case your code should be

date_default_timezone_set('UTC');
$x = strtotime("07/23/2009 18:11");
echo $x;
<?php 

//current time zone to UTC
$date = new DateTime($datetime, new DateTimeZone(date_default_timezone_get()));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone('UTC'));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;

//--------------------------------------------------------------------

//UTC to some other time zone format

$date = new DateTime($datetime, new DateTimeZone("UTC"));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;

IMPORTANT:

In addition to above answers ,there is an important thing that one must follow.Always use the With() function(see below) i.e

Always use :

$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone)); 

Do not use:

$newTimezone = new DateTime($day, new DateTimeZone($timezone));

REASON:(different outputs, check below)

function with($day,$timezone){
    $newTimezone = new DateTime($day);
    $newTimezone->setTimezone(new DateTimeZone($timezone)); 
    $timestamp = $newTimezone->format('U');
    return $timestamp;

}    
function without($day,$timezone){
    $newTimezone = new DateTime($day, new DateTimeZone($timezone));
    $timestamp = $newTimezone->format('U');
    return $timestamp;
}   
     
$tomorrow = date('Y-m-d h:i:s A', strtotime('-1 seconds ' ,strtotime('tomorrow midnight')));
$yesterday = date('Y-m-d h:i:s A', strtotime('+24 hours 1 seconds ' , strtotime('yesterday midnight')));
$timezone = 'UTC';
        


echo 'With Yesterday: '.with($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.with($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

echo 'Without Yesterday: '.without($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.without($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'With Tomorrow: '.with($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.with($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'Without Tomorrow: '.without($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.without($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

OUTPUTS:

With Yesterday: 1537642801

With Yesterday Readable: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM

Without Yesterday: 1537660801

With Yesterday Readable: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM

With Tomorrow: 1537729199

With Yesterday Readable: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM

Without Tomorrow: 1537747199

With Yesterday Readable: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top